package com.lloydm.geosword.common;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
//import java.io.ObjectOutputStream;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.Random;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.util.Log;

import com.threed.jpct.SimpleVector;

//weapon types 1,2,3 standard.
//4 = plasma
//5 = experimental (tractor)
//6 = disrupter
//7 = swarmer missile

public class Simulator
{
     private static final String TAG = "com.lloydm.geosword.common.Simulator";
     private static final String TIMERTAG = "com.lloydm.geosword.Timing";
     private String outputpath = null;
     private String battlefile = null;

     private Random rand = null;
     // to output back to server....
     private String battleresult = null;

     private Ship[] ships = new Ship[32];
     private Projectile[] bullets = new Projectile[1000];
     private int maxbullets = 0;
     private int currentframe = 0;
     private boolean gameover = false;
     private boolean retreating = false;
     // private int startretreat = -1;
     // basic constants...
     // defender team = 0;
     // attacker team = 1;
     private int team1count = 0;
     private int team0count = 0;
     private int dteam = -1;
     private int ateam = -1;
     private KeyFrame[] keyframe = new KeyFrame[8000];
     private CameraKeyFrame[] cameraframe = new CameraKeyFrame[8000];
     private UploadService srvc = null;

     // @SuppressWarnings("unused")
     public Simulator(String input, String opath, UploadService srv, boolean islocal)
     {
                     this.srvc = srv;
                     this.rand = new Random();
                     // input contains all relevant data to the two fleets...
                     // ships etc...weapons, pilots etc etc
                     Log.i(TAG, "Entering simulator. Reading Input");
                     Log.i(TIMERTAG, "TIME(START SIMULATOR):" + System.currentTimeMillis());
                     if (opath != null)
                     {
                                     outputpath = opath;
                                     if (!outputpath.endsWith("/"))
                                     {
                                                     outputpath = outputpath + "/";
                                     }
                     }
                     if (readfrominput(input))
                     {
                                     if (islocal)
                                     {
                                                     getlocalai();
                                     }
                                     Log.i(TIMERTAG, "TIME:(INPUT READ SIMULATOR):" + System.currentTimeMillis());
                                     Log.i(TAG, "Starting Simulator...");
                                     // non dodgy data...go!

                                     // first 250 frames are introduction...
                                     int maxi = 0;
                                     for (int frame = 0; frame < 250; frame++)
                                     {
                                                     for (int i = 0; i < 32; i++)
                                                     {
                                                                     // do nothing yet....
                                                                     if (ships[i] != null)
                                                                     {
                                                                                     ships[i].z += ships[i].vz;
                                                                     }
                                                     }
                                                     maxi = 32;

                                                     /*
                                                         * if (frame == 25 && UploadService.TESTBATTLES) { for (int i = 0; i < 32; i++) { if (ships[i].shiptype != 3) { if ((ships[i].shiptype & UploadService.TESTBATTLEMASK) ==
                                                         * ships[i].shiptype)
{ // all good } else { ships[i].hp = 0; // we died....(testing single // types of ships against // each other) } } else // special case because '3' doesn't work
                                                         * like this.... { if ((UploadService.TESTBATTLEMASK & 8) == 8) {
                                                         *
                                                         * } else { ships[i].hp = 0; } } } }
                                                         *
/
                                                     keyframe[frame] = new KeyFrame();
                                                     keyframe[frame].shipids = new byte[maxi];
                                                     keyframe[frame].shipteam = new byte[maxi];
                                                     keyframe[frame].shiptypes = new byte[maxi];
                                                     keyframe[frame].shipdata = new KeyFrameData[maxi];
                                                     for (int i = 0; i < maxi; i++)
                                                     {
                                                                     if (ships[i] == null)
                                                                     {
                                                                                     continue;
                                                                     }

                                                                     keyframe[frame].shipids[i] = (byte) i;
                                                                     keyframe[frame].shipteam[i] = (byte) ships[i].team;
                                                                     keyframe[frame].shiptypes[i] = (byte) ships[i].shiptype;
                                                                     keyframe[frame].shipdata[i] = new KeyFrameData();
                                                                     keyframe[frame].shipdata[i].x = ships[i].x;
                                                                     keyframe[frame].shipdata[i].y = ships[i].y;
                                                                     keyframe[frame].shipdata[i].z = ships[i].z;
                                                                     keyframe[frame].shipdata[i].firingframe = new byte[ships[i].hardpoints.length];
                                                                     for (int kk = 0; kk < ships[i].hardpoints.length; kk++)
                                                                     {
                                                                                     keyframe[frame].shipdata[i].firingframe[kk] = 0;
                                                                     }
                                                                     keyframe[frame].shipdata[i].hp = (short) ships[i].hp;
                                                     }
                                     }
                                     currentframe = 250;

                                     for (int i = 0; i < 32; i++)
                                     {
                                                     if (ships[i] != null)
                                                     {
                                                                     if (ships[i].hp > 0)
                                                                     {
                                                                                     ships[i].cpuindex = 1;
                                                                     }
                                                     }
                                     }

                                     sendprogressupdate(0.1f);
                                     while (currentframe < 7750 && gameover == false)
                                     {
                                                     /*
                                                         * if(currentframe==251) { Log.i(TAG,"Looking for beam"); for(int i=0;i<32;i++) { for(int j=0;j<ships[i].hardpoints.length;j++) { if(ships[i].hardpoints[j].weapon!=null) {
                                                         * if(ships[i].hardpoints[j].weapon.isbeam) { Log.i(TAG,"BEAM FOUND AT i:"+i+" j:"+j+" on frame 251"); } } } } Log.i(TAG,"Was a beam found check above?"); }
                                                         *
/
                                                     if (currentframe % 300 == 0)
                                                     {
                                                                     // Intent broadcastintent = new
                                                                     // Intent("com.lloydm.geosword.activities.LaunchAttackActivity.progress");
                                                                     // broadcastintent.putExtra("frame", currentframe);
                                                                     // ctx.sendBroadcast(broadcastintent);
                                                                     sendprogressupdate(0.1f + ((float) currentframe / 8100f) * 0.65f);
                                                     }
                                                     // actual battle......
                                                     team1count = 0;
                                                     team0count = 0;
                                                     maxi = 32;
                                                     boolean kamikaze = false;

                                                     // top ranking = ace on each team.... (must be greater than 0
                                                     // hits)
                                                     int aace = -1;
                                                     int dace = -1;

                                                     for (int i = 0; i < 32; i++)
                                                     {

                                                                     if (ships[i] == null)
                                                                     {
                                                                                     // Log.i(TAG,"error null");
                                                                                     continue;
                                                                     }
                                                                     if (ships[i].hp <= 0)
                                                                     {
                                                                                     continue;
                                                                     }
                                                                     ships[i].isace = false;
                                                                     if (ships[i].kills > team1count && ships[i].team == ateam)
                                                                     {
                                                                                     team1count = ships[i].kills;
                                                                                     aace = i;
                                                                     }
                                                                     if (ships[i].kills > team0count && ships[i].team == dteam)
                                                                     {
                                                                                     team0count = ships[i].kills;
                                                                                     dace = i;
                                                                     }
                                                     }
                                                     if (aace != -1)
                                                     {
                                                                     ships[aace].isace = true;
                                                                     // Log.i(TAG,"We have an (attacker) ace! ShipType:"+ships[aace].shiptype);
                                                     }
                                                     if (dace != -1)
                                                     {
                                                                     ships[dace].isace = true;
                                                                     // Log.i(TAG,"We have a (defender) ace! ShipType:"+ships[dace].shiptype);

                                                     }
                                                     team1count = 0;
                                                     team0count = 0;
                                                     for (int i = 0; i < 32; i++)
                                                     {
                                                                     kamikaze = false;
                                                                     if (ships[i] == null)
                                                                     {
                                                                                     // Log.i(TAG,"error null");
                                                                                     continue;
                                                                     }
                                                                     if (ships[i].hp <= 0)
                                                                     {
                                                                                     continue;
                                                                     }
                                                                     maxi = i;
                                                                     if (ships[i].team == ateam && ships[i].iscap == true)
                                                                     {
                                                                                     // new rule - can't launch attack if don't have a
                                                                                     // healthy enough capital ship...
                                                                                     if (ships[i].hp * 10 < ships[i].fullhp && ships[i].hp > 0 && retreating == false)
                                                                                     {
                                                                                                     // retreating = true;
                                                                                                     // startretreat = currentframe;
                                                                                                     // don't want to do this at the moment.....

                                                                                     }
                                                                                     // general retreat......
                                                                                     // retreating = true;
                                                                                     // open warp gates etc....blah blah blah
                                                                     }
                                                                     if (ships[i].team == dteam)
                                                                     {
                                                                                     team0count++;
                                                                     }
                                                                     else
                                                                     {
                                                                                     team1count++;
                                                                     }
                                                                     // really risky ... but nice to build in.....
                                                                     if (ships[i].aislidervalues[1] >= 8)
                                                                     {
                                                                                     kamikaze = true;
                                                                                     if (currentframe == 251)
                                                                                     {
                                                                                                     Log.i(TAG, "Ship:" + i + " is a kamikaze");
                                                                                     }
                                                                     }
                                                                     else
                                                                     {
                                                                                     kamikaze = false;
                                                                     }

                                                                     if (ships[i].cpuindex > 0)
                                                                     {
                                                                                     ships[i].cpuindex--;
                                                                     }
                                                                     if (ships[i].targetshipid >= 0 && ships[i].targetshipid < 32)
                                                                     {
                                                                                     if (ships[ships[i].targetshipid].hp <= 0)
                                                                                     {
                                                                                                     ships[i].targetshipid = -1;
                                                                                     }
                                                                     }
                                                                     // if our cpu indicates time for a new target, or we don't have a target, or we have a target but we haven't shot for the last 90 frames(1.5 second approx) then.....
                                                                     if (ships[i].cpuindex <= 0 || ships[i].targetshipid == -1)
                                                                     {
                                                                                     // check for targets.....

                                                                                     int besttargetid = -1;
                                                                                     int topscore = -10000;
                                                                                     for (int j99 = 0; j99 < 16; j99++)
                                                                                     {
                                                                                                     int j = (i + j99) % 16;
                                                                                                     if (i < 16)
                                                                                                     {
                                                                                                                     j += 16;
                                                                                                     }

                                                                                                     if (ships[j] == null)
                                                                                                     {
                                                                                                                     // Log.i(TAG,"j error:"+j);
                                                                                                                     continue;
                                                                                                     }
                                                                                                     if (ships[j].hp <= 0)
                                                                                                     {
                                                                                                                     continue;
                                                                                                     }
                                                                                                     if (ships[j].team == ships[i].team) // should never happen
                                                                                                     {
                                                                                                                     continue;
                                                                                                     }
                                                                                                     ships[j].targetingscore = 0;

                                                                                                     // go through each slider .. 0 - to 15.....
                                                                                                     // index 0 = islarger (based on ship class really)
                                                                                                     if (ships[j].sizetype > ships[i].sizetype)
                                                                                                     {
                                                                                                                     ships[j].targetingscore += (ships[i].aislidervalues[0] - 5);
                                                                                                                     if (ships[j].sizetype > ships[i].sizetype + 1)
                                                                                                                     {
                                                                                                                                     ships[j].targetingscore += (ships[i].aislidervalues[0] - 5);
                                                                                                                     }
                                                                                                     }
                                                                                                     else
                                                                                                     {
                                                                                                                     if (ships[j].sizetype == ships[i].sizetype)
                                                                                                                     {
                                                                                                                                     // we are the same size.....
                                                                                                                                     // no effect
                                                                                                                     }
                                                                                                                     else
                                                                                                                     {
                                                                                                                                     ships[j].targetingscore -= (ships[i].aislidervalues[0] - 5);
                                                                                                                                     if (ships[j].sizetype < ships[i].sizetype - 1)
                                                                                                                                     {
                                                                                                                                                     ships[j].targetingscore -= (ships[i].aislidervalues[0] - 5);
                                                                                                                                     }
                                                                                                                     }
                                                                                                     }

                                                                                                     float shipdist = (float) Math.sqrt((ships[i].x - ships[j].x) * (ships[i].x - ships[j].x) + (ships[i].y - ships[j].y) * (ships[i].y - ships[j].y) + (ships[i].z - ships[j].z) * (ships[i].z - ships[j].z));
                                                                                                     float myvel = (float) Math.sqrt((ships[i].vx * ships[i].vx) + (ships[i].vy * ships[i].vy) + (ships[i].vz * ships[i].vz));
                                                                                                     float jvel = (float) Math.sqrt((ships[j].vx * ships[j].vx) + (ships[j].vy * ships[j].vy) + (ships[j].vz * ships[j].vz));
                                                                                                     if (shipdist < 250)
                                                                                                     {
                                                                                                                     ships[j].targetingscore += ships[i].aislidervalues[1] - 5;
                                                                                                     }
                                                                                                     else
                                                                                                     {
                                                                                                                     ships[j].targetingscore -= (ships[i].aislidervalues[1] - 5);
                                                                                                     }

                                                                                                     // optimum range...
                                                                                                     int optr = (int) (ships[j].xr + ships[i].xr) * 2;
                                                                                                     if (optr < 600)
                                                                                                     {
                                                                                                                     optr = 600;
                                                                                                     }
                                                                                                     if (shipdist < optr && shipdist > 250)
                                                                                                     {
                                                                                                                     ships[j].targetingscore += ships[i].aislidervalues[2] - 5;
                                                                                                                     // let's refine it a little more
                                                                                                                     if (shipdist < optr && shipdist > (optr * 3) / 4)
                                                                                                                     {
                                                                                                                                     ships[j].targetingscore += ships[i].aislidervalues[2] - 5;
                                                                                                                     }
                                                                                                     }
                                                                                                     else
                                                                                                     {
                                                                                                                     ships[j].targetingscore -= (ships[i].aislidervalues[2] - 5);
                                                                                                     }

                                                                                                     // index 4 = directly ahead
                                                                                                     if (shipdist > 0 && myvel > 0 && jvel > 0)
                                                                                                     {
                                                                                                                     float relposx = (ships[j].x - ships[i].x) / shipdist;
                                                                                                                     float relposy = (ships[j].y - ships[i].y) / shipdist;
                                                                                                                     float relposz = (ships[j].z - ships[i].z) / shipdist;
                                                                                                                     float dotahead = (ships[i].vx * relposx + ships[i].vy * relposy + ships[i].vz * relposz) / myvel;

                                                                                                                     if (dotahead > 0.8)
                                                                                                                     {
                                                                                                                                     ships[j].targetingscore += ships[i].aislidervalues[4] - 5;
                                                                                                                     }
                                                                                                                     else
                                                                                                                     {
                                                                                                                                     ships[j].targetingscore -= (ships[i].aislidervalues[4] - 5);
                                                                                                                     }
                                                                                                                     if (dotahead > 0.1)
                                                                                                                     {
                                                                                                                                     ships[j].targetingscore += ships[i].aislidervalues[5] - 5;
                                                                                                                     }
                                                                                                                     else
                                                                                                                     {
                                                                                                                                     ships[j].targetingscore -= (ships[i].aislidervalues[5] - 5);
                                                                                                                     }
                                                                                                                     if (dotahead < -0.1)
                                                                                                                     {
                                                                                                                                     ships[j].targetingscore += ships[i].aislidervalues[6] - 5;
                                                                                                                     }
                                                                                                                     else
                                                                                                                     {
                                                                                                                                     ships[j].targetingscore -= (ships[i].aislidervalues[6] - 5);
                                                                                                                     }
                                                                                                                     // index 10 = facing us
                                                                                                                     relposx *= -1f;
                                                                                                                     relposy *= -1f;
                                                                                                                     relposz *= -1f;
                                                                                                                     dotahead = (ships[j].vx * relposx + ships[j].vy * relposy + ships[j].vz * relposz) / jvel;
                                                                                                                     if (dotahead > 0.7)
                                                                                                                     {
                                                                                                                                     ships[j].targetingscore += ships[i].aislidervalues[10] - 5;
                                                                                                                     }
                                                                                                                     else
                                                                                                                     {
                                                                                                                                     ships[j].targetingscore -= (ships[i].aislidervalues[10] - 5);
                                                                                                                     }
                                                                                                     }

                                                                                                     // index 7 = shooting at us (as in we are the
                                                                                                     // primary target!)

                                                                                                     if (ships[j].targetshipid == i)
                                                                                                     {
                                                                                                                     ships[j].targetingscore += ships[i].aislidervalues[7] - 5;
                                                                                                     }
                                                                                                     else
                                                                                                     {
                                                                                                                     ships[j].targetingscore -= (ships[i].aislidervalues[7] - 5);
                                                                                                     }
                                                                                                     // of if their weapons are targeting us.....
                                                                                                     if (ships[j].hardpoints != null)
                                                                                                     {
                                                                                                                     for (int j2 = 0; j2 < ships[j].hardpoints.length; j2++)
                                                                                                                     {
                                                                                                                                     if (ships[j].hardpoints[j2].weapon != null)
                                                                                                                                     {
                                                                                                                                                     if (ships[j].hardpoints[j2].weapon.targetshipid == i)
                                                                                                                                                     {
                                                                                                                                                                     ships[j].targetingscore += ships[i].aislidervalues[7] - 5;
                                                                                                                                                     }
                                                                                                                                     }
                                                                                                                     }
                                                                                                     }

                                                                                                     // index 8 = damaged lightly

                                                                                                     if (ships[j].hp * 10 < ships[j].fullhp * 9)
                                                                                                     {
                                                                                                                     ships[j].targetingscore += ships[i].aislidervalues[8] - 5;
                                                                                                                     if (ships[j].hp * 3 < ships[j].fullhp) // severely
                                                                                                                                                                                                                                                                                 // damaged
                                                                                                                     {
                                                                                                                                     ships[j].targetingscore += ships[i].aislidervalues[9] - 5;
                                                                                                                     }
                                                                                                                     else
                                                                                                                     {
                                                                                                                                     ships[j].targetingscore -= (ships[i].aislidervalues[9] - 5);
                                                                                                                     }
                                                                                                     }
                                                                                                     else
                                                                                                     {
                                                                                                                     ships[j].targetingscore -= (ships[i].aislidervalues[8] - 5);
                                                                                                                     ships[j].targetingscore -= (ships[i].aislidervalues[9] - 5);
                                                                                                     }
                                                                                                     // index 11 = enemy targeting our cap
                                                                                                     if (ships[j].targetshipid != -1)
                                                                                                     {
                                                                                                                     int m = ships[j].targetshipid;
                                                                                                                     if (ships[m] != null)
                                                                                                                     {
                                                                                                                                     if (ships[m].hp > 0)
                                                                                                                                     {
                                                                                                                                                     if (ships[m].team == ships[i].team)
                                                                                                                                                     {
                                                                                                                                                                     if (ships[m].iscap)
                                                                                                                                                                     {
                                                                                                                                                                                     ships[j].targetingscore += ships[i].aislidervalues[11] - 5;
                                                                                                                                                                     }
                                                                                                                                                                     else
                                                                                                                                                                     {
                                                                                                                                                                                     ships[j].targetingscore -= (ships[i].aislidervalues[11] - 5);
                                                                                                                                                                     }
                                                                                                                                                     }
                                                                                                                                                     else
                                                                                                                                                     {
                                                                                                                                                                     ships[j].targetingscore -= (ships[i].aislidervalues[11] - 5);
                                                                                                                                                     }
                                                                                                                                     }
                                                                                                                                     else
                                                                                                                                     {
                                                                                                                                                     ships[j].targetingscore -= (ships[i].aislidervalues[11] - 5);
                                                                                                                                     }
                                                                                                                     }
                                                                                                                     else
                                                                                                                     {
                                                                                                                                     ships[j].targetingscore -= (ships[i].aislidervalues[11] - 5);
                                                                                                                     }
                                                                                                     }
                                                                                                     else
                                                                                                     {
                                                                                                                     ships[j].targetingscore -= (ships[i].aislidervalues[11] - 5);
                                                                                                     }
                                                                                                     // index 12 = enemy at very long range
                                                                                                     if (shipdist > 1000)
                                                                                                     {
                                                                                                                     ships[j].targetingscore += ships[i].aislidervalues[12] - 5;
                                                                                                                     if (shipdist > 2000)
                                                                                                                     {
                                                                                                                                     ships[j].targetingscore += ships[i].aislidervalues[12] - 5;
                                                                                                                     }
                                                                                                     }
                                                                                                     else
                                                                                                     {
                                                                                                                     ships[j].targetingscore -= (ships[i].aislidervalues[12] - 5);
                                                                                                     }

                                                                                                     // index 13 = enemy faster than us (current speed)
                                                                                                     if (jvel > myvel)
                                                                                                     {
                                                                                                                     ships[j].targetingscore += ships[i].aislidervalues[13] - 5;
                                                                                                     }
                                                                                                     else
                                                                                                     {
                                                                                                                     ships[j].targetingscore -= (ships[i].aislidervalues[13] - 5);
                                                                                                     }
                                                                                                     // index 14 = enemy more agile than us (accel)
                                                                                                     if (ships[j].maxaccel > ships[i].maxaccel)
                                                                                                     {
                                                                                                                     ships[j].targetingscore += ships[i].aislidervalues[14] - 5;
                                                                                                     }
                                                                                                     else
                                                                                                     {
                                                                                                                     ships[j].targetingscore -= (ships[i].aislidervalues[14] - 5);
                                                                                                     }
                                                                                                     // index 15 = enemy is an ace -> determined in the
                                                                                                     // battle itself based on hits on enemy
                                                                                                     // opponents....
                                                                                                     if (ships[j].isace)
                                                                                                     {
                                                                                                                     ships[j].targetingscore += ships[i].aislidervalues[15] - 5;
                                                                                                     }
                                                                                                     else
                                                                                                     {
                                                                                                                     ships[j].targetingscore -= (ships[i].aislidervalues[15] - 5);
                                                                                                     }
                                                                                                     if (ships[j].targetingscore > topscore)
                                                                                                     {
                                                                                                                     besttargetid = j;
                                                                                                                     topscore = ships[j].targetingscore;

                                                                                                     }
                                                                                     }
                                                                                     if (besttargetid != -1)
                                                                                     {
                                                                                                     if (besttargetid != ships[i].targetshipid)
                                                                                                     {
                                                                                                                     ships[i].forcewait = false;
                                                                                                     }
                                                                                                     ships[i].targetshipid = besttargetid;
                                                                                     }
                                                                                     ships[i].cpuindex = 600 + (ships[i].aislidervalues[3] - 5) * 100;// ships[i].cputime;
                                                                                     if (ships[i].cpuindex < 100)
                                                                                     {
                                                                                                     ships[i].cpuindex = 100;
                                                                                     }
                                                                                     if (ships[i].cpuindex > 1000)
                                                                                     {
                                                                                                     ships[i].cpuindex = 1000;
                                                                                     }
                                                                     }

                                                                     float distfromorigin = (float) Math.sqrt(ships[i].x * ships[i].x + ships[i].y * ships[i].y + ships[i].z * ships[i].z);
                                                                     float myvel = (float) Math.sqrt((ships[i].vx * ships[i].vx) + (ships[i].vy * ships[i].vy) + (ships[i].vz * ships[i].vz));

                                                                     if (distfromorigin > 10000 && myvel > 0)
                                                                     {
                                                                                     // head towards the centre.....
                                                                                     // ships[i].targetshipid = -1;
                                                                                     float ndx = ships[i].vx - ships[i].x * ships[i].maxaccel / 10000f;
                                                                                     float ndy = ships[i].vy - ships[i].y * ships[i].maxaccel / 10000f;
                                                                                     float ndz = ships[i].vz - ships[i].z * ships[i].maxaccel / 10000f;

                                                                                     float nlen = (float) Math.sqrt(ndx * ndx + ndy * ndy + ndz * ndz);
                                                                                     if (nlen > ships[i].maxvel)
                                                                                     {
                                                                                                     ndx /= nlen;
                                                                                                     ndy /= nlen;
                                                                                                     ndz /= nlen;
                                                                                                     ndx *= ships[i].maxvel;
                                                                                                     ndy *= ships[i].maxvel;
                                                                                                     ndz *= ships[i].maxvel;
                                                                                     }
                                                                                     else
                                                                                     {
                                                                                                     if (nlen > 0 && nlen < ships[i].minvel)
                                                                                                     {
                                                                                                                     ndx /= nlen;
                                                                                                                     ndy /= nlen;
                                                                                                                     ndz /= nlen;
                                                                                                                     ndx *= ships[i].minvel;
                                                                                                                     ndy *= ships[i].minvel;
                                                                                                                     ndz *= ships[i].minvel;

                                                                                                     }
                                                                                                     else
                                                                                                     {
                                                                                                                     if (nlen == 0)
                                                                                                                     {
                                                                                                                                     ndx = 0.001f;
                                                                                                                                     ndy = 0.001f;
                                                                                                                                     ndz = 0.001f;
                                                                                                                     }
                                                                                                     }
                                                                                     }
                                                                                     ships[i].vy = ndy;
                                                                                     ships[i].vz = ndz;
                                                                                     ships[i].vx = ndx;

                                                                     }// end of outside game bounds

                                                                     if (ships[i].forcewait)
                                                                     {
                                                                                     if (currentframe - ships[i].lastshotframe > 200)
                                                                                     {
                                                                                                     ships[i].forcewait = false;
                                                                                     }
                                                                     }
                                                                     if (ships[i].targetshipid != -1) // ie we have a target.....
                                                                     {
                                                                                     int j = ships[i].targetshipid;
                                                                                     if (ships[j] != null)
                                                                                     {
                                                                                                     if (ships[j].hp > 0)
                                                                                                     {
                                                                                                                     if (ships[i].forcewait == false)
                                                                                                                     {
                                                                                                                                     // set our target velocity and direction towards
                                                                                                                                     // this ship.....
                                                                                                                                     float shipdist = (float) Math.sqrt((ships[i].x - ships[j].x) * (ships[i].x - ships[j].x) + (ships[i].y - ships[j].y) * (ships[i].y - ships[j].y) + (ships[i].z - ships[j].z) * (ships[i].z - ships[j].z));
                                                                                                                                     if (shipdist > 0)
                                                                                                                                     {
                                                                                                                                                     float tdx = (ships[j].x - ships[i].x) / shipdist;
                                                                                                                                                     float tdy = (ships[j].y - ships[i].y) / shipdist;
                                                                                                                                                     float tdz = (ships[j].z - ships[i].z) / shipdist;

                                                                                                                                                     float ndx = ships[i].vx + tdx * ships[i].maxaccel;
                                                                                                                                                     float ndy = ships[i].vy + tdy * ships[i].maxaccel;
                                                                                                                                                     float ndz = ships[i].vz + tdz * ships[i].maxaccel;

                                                                                                                                                     float nlen = (float) Math.sqrt(ndx * ndx + ndy * ndy + ndz * ndz);
                                                                                                                                                     if (nlen > ships[i].maxvel)
                                                                                                                                                     {
                                                                                                                                                                     ndx /= nlen;
                                                                                                                                                                     ndy /= nlen;
                                                                                                                                                                     ndz /= nlen;
                                                                                                                                                                     ndx *= ships[i].maxvel;
                                                                                                                                                                     ndy *= ships[i].maxvel;
                                                                                                                                                                     ndz *= ships[i].maxvel;
                                                                                                                                                     }
                                                                                                                                                     else
                                                                                                                                                     {
                                                                                                                                                                     if (nlen < ships[i].minvel && nlen > 0)
                                                                                                                                                                     {
                                                                                                                                                                                     ndx /= nlen;
                                                                                                                                                                                     ndy /= nlen;
                                                                                                                                                                                     ndz /= nlen;
                                                                                                                                                                                     ndx *= ships[i].minvel;
                                                                                                                                                                                     ndy *= ships[i].minvel;
                                                                                                                                                                                     ndz *= ships[i].minvel;
                                                                                                                                                                     }
                                                                                                                                                                     else
                                                                                                                                                                     {
                                                                                                                                                                                     if (nlen == 0)
                                                                                                                                                                                     {
                                                                                                                                                                                                     ndx = 0.001f;
                                                                                                                                                                                                     ndy = 0.001f;
                                                                                                                                                                                                     ndz = 0.001f;
                                                                                                                                                                                     }
                                                                                                                                                                     }
                                                                                                                                                     }
                                                                                                                                                     ships[i].vy = ndy;
                                                                                                                                                     ships[i].vz = ndz;
                                                                                                                                                     ships[i].vx = ndx;
                                                                                                                                     }
                                                                                                                     }
                                                                                                     }
                                                                                                     else
                                                                                                     {
                                                                                                                     ships[i].targetshipid = -1;
                                                                                                     }
                                                                                     }
                                                                                     else
                                                                                     {
                                                                                                     ships[i].targetshipid = -1;
                                                                                     }
                                                                     }

                                                                     // repulsive force when it comes to nearby units....
                                                                     for (int j = 0; j < 32; j++)
                                                                     {
                                                                                     if (i == j)
                                                                                     {
                                                                                                     continue;
                                                                                     }
                                                                                     if (ships[j] != null)
                                                                                     {
                                                                                                     if (ships[j].hp > 0)
                                                                                                     {
                                                                                                                     float shipdist = (float) Math.sqrt((ships[i].x - ships[j].x) * (ships[i].x - ships[j].x) + (ships[i].y - ships[j].y) * (ships[i].y - ships[j].y) + (ships[i].z - ships[j].z) * (ships[i].z - ships[j].z));
                                                                                                                     if (shipdist > 0 && iwillcollidewithyou(i, j) == true) // calculate if we will collide with the enemy!
                                                                                                                     // if we are a kamikaze then fine...go ahead and do so....otherwise we don't want to do this!

                                                                                                                     {
                                                                                                                                     if (kamikaze && ships[j].team != ships[i].team)
                                                                                                                                     {
                                                                                                                                                     // collide and do damage if we are
                                                                                                                                                     // within really small distance....
                                                                                                                                                     if (shipdist < (ships[i].xr + ships[j].xr) * 0.3f)
                                                                                                                                                     {
                                                                                                                                                                     // smaller ship is destroyed....
                                                                                                                                                                     // larger ship takes damage.....
                                                                                                                                                                     // based on fullhp value.....
                                                                                                                                                                     ships[i].hp -= ships[j].fullhp * ships[j].maxvel; // the faster we are going, with the more mass...the more damage we do!
                                                                                                                                                                     ships[j].hp -= ships[i].fullhp * ships[i].maxvel; // so yeah flying bombs is what you can turn little ships into! (and big ships!)
                                                                                                                                                                     // however little ships have no chance when they collide.....
                                                                                                                                                                     if (ships[i].shiptype > 2)
                                                                                                                                                                     {
                                                                                                                                                                                     ships[i].hp = 0;
                                                                                                                                                                     }
                                                                                                                                                                     if (ships[j].shiptype > 2)
                                                                                                                                                                     {
                                                                                                                                                                                     ships[j].hp = 0;
                                                                                                                                                                     }
                                                                                                                                                                     // the other thing you need to do is stick together....
                                                                                                                                                                     // ie the ship that survives has its velocity altered....pushed....
                                                                                                                                                                    
                                                                                                                                                                     /*
                                                                                                                                                                     if (ships[i].hp > 0) // the plus 1 is to prevent div by zero errors....
                                                                                                                                                                     {
                                                                                                                                                                                     ships[i].vx += 0.5f * ships[j].vx * (ships[i].fullhp / (1 + ships[j].fullhp));
                                                                                                                                                                                     ships[i].vy += 0.5f * ships[j].vy * (ships[i].fullhp / (1 + ships[j].fullhp));
                                                                                                                                                                                     ships[i].vz += 0.5f * ships[j].vz * (ships[i].fullhp / (1 + ships[j].fullhp));
                                                                                                                                                                     }
                                                                                                                                                                     if (ships[j].hp > 0)
                                                                                                                                                                     {
                                                                                                                                                                                     ships[j].vx += 0.5f * ships[i].vx * (ships[j].fullhp / (1 + ships[i].fullhp));
                                                                                                                                                                                     ships[j].vy += 0.5f * ships[i].vy * (ships[j].fullhp / (1 + ships[i].fullhp));
                                                                                                                                                                                     ships[j].vz += 0.5f * ships[i].vz * (ships[j].fullhp / (1 + ships[i].fullhp));
                                                                                                                                                                     }
                                                                                                                                                                     *
/
                                                                                                                                                                    
                                                                                                                                                     }

                                                                                                                                     }
                                                                                                                                     else
                                                                                                                                     {
                                                                                                                                                     float relx = (ships[i].x - ships[j].x) / shipdist;
                                                                                                                                                     float rely = (ships[i].y - ships[j].y) / shipdist;
                                                                                                                                                     float relz = (ships[i].z - ships[j].z) / shipdist;

                                                                                                                                                     ships[i].vy += relx * ships[i].maxaccel;
                                                                                                                                                     ships[i].vx += rely * ships[i].maxaccel;
                                                                                                                                                     ships[i].vz += relz * ships[i].maxaccel;

                                                                                                                                                     float ndx = ships[i].vx;
                                                                                                                                                     float ndy = ships[i].vy;
                                                                                                                                                     float ndz = ships[i].vz;

                                                                                                                                                     float nlen = (float) Math.sqrt(ndx * ndx + ndy * ndy + ndz * ndz);
                                                                                                                                                     if (nlen > ships[i].maxvel)
                                                                                                                                                     {
                                                                                                                                                                     ndx /= nlen;
                                                                                                                                                                     ndy /= nlen;
                                                                                                                                                                     ndz /= nlen;
                                                                                                                                                                     ndx *= ships[i].maxvel;
                                                                                                                                                                     ndy *= ships[i].maxvel;
                                                                                                                                                                     ndz *= ships[i].maxvel;
                                                                                                                                                     }
                                                                                                                                                     if (nlen == 0)
                                                                                                                                                     {
                                                                                                                                                                     ndx = 0.001f;
                                                                                                                                                                     ndy = 0.001f;
                                                                                                                                                                     ndz = 0.001f;
                                                                                                                                                     }

                                                                                                                                                     ships[i].vx = ndx;
                                                                                                                                                     ships[i].vy = ndy;
                                                                                                                                                     ships[i].vz = ndz;
                                                                                                                                                     if (ships[i].targetshipid == j)
                                                                                                                                                     {
                                                                                                                                                                     ships[i].forcewait = true;
                                                                                                                                                                     ships[i].lastshotframe = currentframe;

                                                                                                                                                     }

                                                                                                                                     }
                                                                                                                     }
                                                                                                     }
                                                                                     }
                                                                     }

                                                                     // fire weapons (before any movement takes place...)
                                                                     // assuming power drain etc is all okay...

                                                                     if (ships[i].powerlevel < 100)
                                                                     {
                                                                                     ships[i].powerlevel += ships[i].rechargerate; // 1
                                                                                                                                                                                                                                                                             // now...
                                                                     }
                                                                     else
                                                                     {
                                                                                     ships[i].powerlevel = 100;
                                                                     }

                                                                     for (int k = 0; k < ships[i].hardpoints.length; k++)
                                                                     {
                                                                                     if (ships[i].hardpoints[k] != null && ships[i].hp > 0)
                                                                                     {
                                                                                                     if (ships[i].hardpoints[k].hp > 0)
                                                                                                     {
                                                                                                                     if (ships[i].hardpoints[k].weapon != null)
                                                                                                                     {
                                                                                                                                     if (ships[i].powerlevel >= ships[i].hardpoints[k].weapon.powerdrain)
                                                                                                                                     {
                                                                                                                                                     if (ships[i].hardpoints[k].weapon.ammoleft > 0 || ships[i].hardpoints[k].weapon.ammolimit <= 0)
                                                                                                                                                     {

                                                                                                                                                                     if (ships[i].hardpoints[k].weapon.cooldownindex <= 0)
                                                                                                                                                                     {
                                                                                                                                                                                     ships[i].hardpoints[k].weapon.targetshipid = -1;
                                                                                                                                                                                     if (targetcalc(i, k))
                                                                                                                                                                                     {
                                                                                                                                                                                                     if (ships[i].hardpoints[k].weapon.locktimeindex >= ships[i].hardpoints[k].weapon.locktimerequired)
                                                                                                                                                                                                     {

                                                                                                                                                                                                                     ships[i].hardpoints[k].weapon.locktimeindex = 0;
                                                                                                                                                                                                                     if (ships[i].hardpoints[k].weapon.ammolimit > 0)
                                                                                                                                                                                                                     {
                                                                                                                                                                                                                                     ships[i].hardpoints[k].weapon.ammoleft--;
                                                                                                                                                                                                                     }
                                                                                                                                                                                                                     ships[i].powerlevel -= ships[i].hardpoints[k].weapon.powerdrain;
                                                                                                                                                                                                                     ships[i].hardpoints[k].weapon.cooldownindex = ships[i].hardpoints[k].weapon.cooldowntimerequired;
                                                                                                                                                                                                                     ships[i].hardpoints[k].weapon.fireframe = currentframe;

                                                                                                                                                                                                                     for (int j = 0; j < 1000; j++)
                                                                                                                                                                                                                     {
                                                                                                                                                                                                                                     if (bullets[j] == null)
                                                                                                                                                                                                                                     {
                                                                                                                                                                                                                                                     Projectile b = new Projectile();
                                                                                                                                                                                                                                                     if(j+1>maxbullets)
                                                                                                                                                                                                                                                     {
                                                                                                                                                                                                                                                                     maxbullets = j+1;
                                                                                                                                                                                                                                                     }
                                                                                                                                                                                                                                                     b.damageonhit = ships[i].hardpoints[k].weapon.damageperhit;
                                                                                                                                                                                                                                                     b.lifetime = ships[i].hardpoints[k].weapon.baselifetime;
                                                                                                                                                                                                                                                     if (ships[i].hardpoints[k].weapon.weapontype == 5)
                                                                                                                                                                                                                                                     {
                                                                                                                                                                                                                                                                     b.istractor = true;
                                                                                                                                                                                                                                                     }
                                                                                                                                                                                                                                                     if (ships[i].hardpoints[k].weapon.weapontype == 6)
                                                                                                                                                                                                                                                     {
                                                                                                                                                                                                                                                                     b.isdisruptor = true;
                                                                                                                                                                                                                                                     }

                                                                                                                                                                                                                                                     // position of
                                                                                                                                                                                                                                                     // gun muzzle...
                                                                                                                                                                                                                                                     SimpleVector vec = new SimpleVector(ships[i].hardpoints[k].x, ships[i].hardpoints[k].y, ships[i].hardpoints[k].z);
                                                                                                                                                                                                                                                     SimpleVector vec2 = new SimpleVector(ships[i].vx, ships[i].vy, ships[i].vz);
                                                                                                                                                                                                                                                     vec2 = vec2.normalize(vec2);
                                                                                                                                                                                                                                                     vec.rotate(vec2.getRotationMatrix());
                                                                                                                                                                                                                                                     b.x = ships[i].x + vec.x;
                                                                                                                                                                                                                                                     b.y = ships[i].y + vec.y;
                                                                                                                                                                                                                                                     b.z = ships[i].z + vec.z;

                                                                                                                                                                                                                                                     vec = new SimpleVector(ships[i].hardpoints[k].dx, ships[i].hardpoints[k].dy, ships[i].hardpoints[k].dz);

                                                                                                                                                                                                                                                     vec.rotate(vec2.getRotationMatrix());

                                                                                                                                                                                                                                                     b.vx = ships[i].vx + vec.x * ships[i].hardpoints[k].weapon.muzzlevel; // ships[i].hardpoints[k].weapon.dx;
                                                                                                                                                                                                                                                     b.vy = ships[i].vy + vec.y * ships[i].hardpoints[k].weapon.muzzlevel; // ships[i].hardpoints[k].weapon.dx;
                                                                                                                                                                                                                                                     b.vz = ships[i].vz + vec.z * ships[i].hardpoints[k].weapon.muzzlevel; // ships[i].hardpoints[k].weapon.dx;

                                                                                                                                                                                                                                                     b.maxvel = ships[i].hardpoints[k].weapon.muzzlevel;

                                                                                                                                                                                                                                                     b.owningshipid = i;
                                                                                                                                                                                                                                                     b.owningshipteam = ships[i].team;

                                                                                                                                                                                                                                                     b.isbeam = ships[i].hardpoints[k].weapon.isbeam;
                                                                                                                                                                                                                                                     if (b.lifetime < 2 && ships[i].hardpoints[k].weapon.isbeam == false)
                                                                                                                                                                                                                                                     {
                                                                                                                                                                                                                                                                     b.isbeam = true;
                                                                                                                                                                                                                                                     }
                                                                                                                                                                                                                                                     b.isguided = ships[i].hardpoints[k].weapon.isguided;
                                                                                                                                                                                                                                                     b.targetshipid = ships[i].hardpoints[k].weapon.targetshipid;
                                                                                                                                                                                                                                                     b.guidedturnrate = ships[i].hardpoints[k].weapon.guidedturnrate;
                                                                                                                                                                                                                                                     b.ismissile = ships[i].hardpoints[k].weapon.ismissile;
                                                                                                                                                                                                                                                     bullets[j] = b;

                                                                                                                                                                                                                                                     if (b.isbeam)
                                                                                                                                                                                                                                                     {
                                                                                                                                                                                                                                                                     Log.i(TAG, "Beam fired");
                                                                                                                                                                                                                                                                     b.lifetime = 2;
                                                                                                                                                                                                                                                                     b.vx = ships[b.targetshipid].x - b.x;
                                                                                                                                                                                                                                                                     b.vy = ships[b.targetshipid].y - b.y;
                                                                                                                                                                                                                                                                     b.vz = ships[b.targetshipid].z - b.z;
                                                                                                                                                                                                                                                                     b.maxvel = 1000f;
                                                                                                                                                                                                                                                     }

                                                                                                                                                                                                                                                     ships[i].hardpoints[k].fx = b.x;
                                                                                                                                                                                                                                                     ships[i].hardpoints[k].fy = b.y;
                                                                                                                                                                                                                                                     ships[i].hardpoints[k].fz = b.z;

                                                                                                                                                                                                                                                     ships[i].hardpoints[k].fux = b.vx;
                                                                                                                                                                                                                                                     ships[i].hardpoints[k].fuy = b.vy;
                                                                                                                                                                                                                                                     ships[i].hardpoints[k].fuz = b.vz;

                                                                                                                                                                                                                                                     break;
                                                                                                                                                                                                                                     }
                                                                                                                                                                                                                                     if (bullets[j].lifetime <= 0)
                                                                                                                                                                                                                                     {
                                                                                                                                                                                                                                                     Projectile b = bullets[j];
                                                                                                                                                                                                                                                     b.damageonhit = ships[i].hardpoints[k].weapon.damageperhit;
                                                                                                                                                                                                                                                     b.lifetime = ships[i].hardpoints[k].weapon.baselifetime;
                                                                                                                                                                                                                                                     // position of
                                                                                                                                                                                                                                                     // gun muzzle...
                                                                                                                                                                                                                                                     SimpleVector vec = new SimpleVector(ships[i].hardpoints[k].x, ships[i].hardpoints[k].y, ships[i].hardpoints[k].z);
                                                                                                                                                                                                                                                     SimpleVector vec2 = new SimpleVector(ships[i].vx, ships[i].vy, ships[i].vz);
                                                                                                                                                                                                                                                     vec2 = vec2.normalize(vec2);
                                                                                                                                                                                                                                                     vec.rotate(vec2.getRotationMatrix());

                                                                                                                                                                                                                                                     b.x = ships[i].x + vec.x;
                                                                                                                                                                                                                                                     b.y = ships[i].y + vec.y;
                                                                                                                                                                                                                                                     b.z = ships[i].z + vec.z;

                                                                                                                                                                                                                                                     vec = new SimpleVector(ships[i].hardpoints[k].dx, ships[i].hardpoints[k].dy, ships[i].hardpoints[k].dz);

                                                                                                                                                                                                                                                     vec.rotate(vec2.getRotationMatrix());

                                                                                                                                                                                                                                                     b.vx = ships[i].vx + vec.x * ships[i].hardpoints[k].weapon.muzzlevel; // ships[i].hardpoints[k].weapon.dx;
                                                                                                                                                                                                                                                     b.vy = ships[i].vy + vec.y * ships[i].hardpoints[k].weapon.muzzlevel; // ships[i].hardpoints[k].weapon.dx;
                                                                                                                                                                                                                                                     b.vz = ships[i].vz + vec.z * ships[i].hardpoints[k].weapon.muzzlevel; // ships[i].hardpoints[k].weapon.dx;

                                                                                                                                                                                                                                                     b.maxvel = ships[i].hardpoints[k].weapon.muzzlevel;
                                                                                                                                                                                                                                                     b.owningshipid = i;
                                                                                                                                                                                                                                                     b.owningshipteam = ships[i].team;
                                                                                                                                                                                                                                                     b.targetshipid = ships[i].hardpoints[k].weapon.targetshipid;
                                                                                                                                                                                                                                                     b.isbeam = ships[i].hardpoints[k].weapon.isbeam;
                                                                                                                                                                                                                                                     if (b.lifetime < 2 && ships[i].hardpoints[k].weapon.isbeam == false)
                                                                                                                                                                                                                                                     {
                                                                                                                                                                                                                                                                     b.isbeam = true;
                                                                                                                                                                                                                                                     }

                                                                                                                                                                                                                                                     if (ships[i].hardpoints[k].weapon.weapontype == 5)
                                                                                                                                                                                                                                                     {
                                                                                                                                                                                                                                                                     b.istractor = true;
                                                                                                                                                                                                                                                     }
                                                                                                                                                                                                                                                     else
                                                                                                                                                                                                                                                     {
                                                                                                                                                                                                                                                                     b.istractor = false;
                                                                                                                                                                                                                                                     }
                                                                                                                                                                                                                                                     if (ships[i].hardpoints[k].weapon.weapontype == 6)
                                                                                                                                                                                                                                                     {
                                                                                                                                                                                                                                                                     b.isdisruptor = true;
                                                                                                                                                                                                                                                     }
                                                                                                                                                                                                                                                     else
                                                                                                                                                                                                                                                     {
                                                                                                                                                                                                                                                                     b.isdisruptor = false;
                                                                                                                                                                                                                                                     }

                                                                                                                                                                                                                                                     if (b.isbeam)
                                                                                                                                                                                                                                                     {
                                                                                                                                                                                                                                                                     Log.i(TAG, "Beam fired");
                                                                                                                                                                                                                                                                     b.vx = ships[b.targetshipid].x - b.x;
                                                                                                                                                                                                                                                                     b.vy = ships[b.targetshipid].y - b.y;
                                                                                                                                                                                                                                                                     b.vz = ships[b.targetshipid].z - b.z;
                                                                                                                                                                                                                                                                     b.lifetime = 2;
                                                                                                                                                                                                                                                                     b.maxvel = 1000f;
                                                                                                                                                                                                                                                     }

                                                                                                                                                                                                                                                     b.isguided = ships[i].hardpoints[k].weapon.isguided;
                                                                                                                                                                                                                                                     b.guidedturnrate = ships[i].hardpoints[k].weapon.guidedturnrate;
                                                                                                                                                                                                                                                     b.ismissile = ships[i].hardpoints[k].weapon.ismissile;
                                                                                                                                                                                                                                                     bullets[j] = b;
                                                                                                                                                                                                                                                     ships[i].hardpoints[k].fx = b.x;
                                                                                                                                                                                                                                                     ships[i].hardpoints[k].fy = b.y;
                                                                                                                                                                                                                                                     ships[i].hardpoints[k].fz = b.z;

                                                                                                                                                                                                                                                     ships[i].hardpoints[k].fux = b.vx;
                                                                                                                                                                                                                                                     ships[i].hardpoints[k].fuy = b.vy;
                                                                                                                                                                                                                                                     ships[i].hardpoints[k].fuz = b.vz;
                                                                                                                                                                                                                                                     break;
                                                                                                                                                                                                                                     }
                                                                                                                                                                                                                     }

                                                                                                                                                                                                     }
                                                                                                                                                                                                     else
                                                                                                                                                                                                     {
                                                                                                                                                                                                                     ships[i].hardpoints[k].weapon.locktimeindex++;
                                                                                                                                                                                                                     // Log.i(TAG,"Ship acquiring lock");
                                                                                                                                                                                                     }
                                                                                                                                                                                     }

                                                                                                                                                                     }
                                                                                                                                                                     else
                                                                                                                                                                     {
                                                                                                                                                                                     ships[i].hardpoints[k].weapon.cooldownindex--;
                                                                                                                                                                     }

                                                                                                                                                     }
                                                                                                                                                     else
                                                                                                                                                     {
                                                                                                                                                                     // Log.i(TAG,"Ship ammo low/problem");
                                                                                                                                                     }
                                                                                                                                     }
                                                                                                                                     else
                                                                                                                                     {
                                                                                                                                                     // Log.i(TAG,"Ship power too low");
                                                                                                                                     }
                                                                                                                     }
                                                                                                                     else
                                                                                                                     {
                                                                                                                                     // Log.i(TAG,"Hard point weapon is null");
                                                                                                                     }
                                                                                                     }
                                                                                                     else
                                                                                                     {
                                                                                                                     // Log.i(TAG,"Hard point hp zero");
                                                                                                     }
                                                                                     }
                                                                     }

                                                                     // movement...
                                                                     ships[i].x += ships[i].vx;
                                                                     ships[i].y += ships[i].vy;
                                                                     ships[i].z += ships[i].vz;

                                                                     // recalculate bounding box.....

                                                                     // cheat for now....since no idea if the up vectors will
                                                                     // match between this and the replay....
                                                                     // yep need a bloody up vector as well as a forwards vector
                                                                     // to define the orientation...
                                                                     // of the bounding box..

                                                     }

                                                     for (int i = 0; i < maxbullets; i++)
                                                     {
                                                                     if (bullets[i] == null)
                                                                     {
                                                                                     break;
                                                                     }

                                                                     if (bullets[i].lifetime > 0)
                                                                     {
                                                                                     bullets[i].lifetime--;
                                                                     }
                                                                     else
                                                                     {
                                                                                     continue;
                                                                     }

                                                                     if (bullets[i].ismissile)
                                                                     {
                                                                     }

                                                                     if (!bullets[i].isbeam)
                                                                     {
                                                                                     bullets[i].x += bullets[i].vx;
                                                                                     bullets[i].y += bullets[i].vy;
                                                                                     bullets[i].z += bullets[i].vz;
                                                                     }
                                                                     if (bullets[i].isguided && bullets[i].ismissile)
                                                                     {
                                                                                     if (bullets[i].targetshipid != -1)
                                                                                     {
                                                                                                     // adjust vx,vy,vz accordingly.....
                                                                                                     int j = bullets[i].targetshipid;
                                                                                                     if (ships[j] != null)
                                                                                                     {
                                                                                                                     if (ships[j].hp > 0)
                                                                                                                     {
                                                                                                                                     // okay so adjust ever so slightly.....
                                                                                                                                     float dx = ships[j].x - bullets[i].x;
                                                                                                                                     float dy = ships[j].y - bullets[i].y;
                                                                                                                                     float dz = ships[j].z - bullets[i].z;

                                                                                                                                     float l = (float) Math.sqrt(dx * dx + dy * dy + dz * dz);
                                                                                                                                     if (l > 0)
                                                                                                                                     {
                                                                                                                                                     dx /= l;
                                                                                                                                                     dy /= l;
                                                                                                                                                     dz /= l;

                                                                                                                                                     float bvx = bullets[i].vx;
                                                                                                                                                     float bvy = bullets[i].vy;
                                                                                                                                                     float bvz = bullets[i].vz;
                                                                                                                                                     float bl = (float) Math.sqrt(bvx * bvx + bvy * bvy + bvz * bvz);
                                                                                                                                                     if (bl > 0)
                                                                                                                                                     {
                                                                                                                                                                     bvx /= bl;
                                                                                                                                                                     bvy /= bl;
                                                                                                                                                                     bvz /= bl;
                                                                                                                                                     }
                                                                                                                                                     float nvx = bvx + (dx) * bullets[i].guidedturnrate;
                                                                                                                                                     float nvy = bvy + (dy) * bullets[i].guidedturnrate;
                                                                                                                                                     float nvz = bvz + (dz) * bullets[i].guidedturnrate;
                                                                                                                                                     float nl = (float) Math.sqrt(nvx * nvx + nvy * nvy + nvz * nvz);
                                                                                                                                                     if (nl > 0)
                                                                                                                                                     {
                                                                                                                                                                     nvx /= nl;
                                                                                                                                                                     nvy /= nl;
                                                                                                                                                                     nvz /= nl;
                                                                                                                                                     }
                                                                                                                                                     bullets[i].vx = nvx * bullets[i].maxvel;
                                                                                                                                                     bullets[i].vy = nvy * bullets[i].maxvel;
                                                                                                                                                     bullets[i].vz = nvz * bullets[i].maxvel;
                                                                                                                                     }
                                                                                                                     }
                                                                                                     }

                                                                                     }
                                                                     }
                                                                     // check for collisions...
                                                                     for (int j = 0; j < 32; j++)
                                                                     {
                                                                                     if (ships[j] == null)
                                                                                     {
                                                                                                     continue;
                                                                                     }
                                                                                     if (ships[j].hp <= 0)
                                                                                     {
                                                                                                     continue;
                                                                                     }
                                                                                     if (bullets[i].isbeam)
                                                                                     {
                                                                                                     if (bullets[i].targetshipid == j)
                                                                                                     {
                                                                                                                     // automatically damage....
                                                                                                                     ships[j].hp -= bullets[i].damageonhit;
                                                                                                                     if (ships[j].hp > 0)
                                                                                                                     {
                                                                                                                                     Log.i(TAG, "Ship Damaged by BEAM!");
                                                                                                                     }
                                                                                                                     else
                                                                                                                     {
                                                                                                                                     Log.i(TAG, "Ship Destroyed by BEAM!:" + j + " Ship Type:" + ships[j].shiptype + " Ship Size:" + ships[j].sizetype + " Ship full Hp:" + ships[j].fullhp);
                                                                                                                                     if (ships[j].shiptype < 3)
                                                                                                                                     {
                                                                                                                                                     ships[j].hp = -10000;
                                                                                                                                                     // special marker to split in half....
                                                                                                                                     }
                                                                                                                                     if (bullets[i].owningshipid >= 0 && bullets[i].owningshipid < 32)
                                                                                                                                     {
                                                                                                                                                     // for special effect (do later though - not tonight)
                                                                                                                                                     // on certain rare occasions split the target ship down the middle....
                                                                                                                                                     // in order to signal to the replay viewer that is what we want do do we need to specify
                                                                                                                                                     // some flag that get will get passed across...
                                                                                                                                                     // the easiest one is the hp....
                                                                                                                                                     // hp in keyframe data is a short...so we can use the negative numbers and specify a value...
                                                                                                                                                     // however I don't want to do it yet since I'm not sure how the system will handle
                                                                                                                                                     // negative numbers like that......
                                                                                                                                                     // alternatively we do it another way...

                                                                                                                                                     ships[bullets[i].owningshipid].kills++;
                                                                                                                                                     ships[bullets[i].owningshipid].hits += 3; // more
                                                                                                                                                                                                                                                                                                                             // points
                                                                                                                                                                                                                                                                                                                             // for
                                                                                                                                                                                                                                                                                                                             // landing
                                                                                                                                                                                                                                                                                                                             // the
                                                                                                                                                                                                                                                                                                                             // final
                                                                                                                                                                                                                                                                                                                             // blow...
                                                                                                                                     }
                                                                                                                     }
                                                                                                                     if (bullets[i].owningshipid >= 0 && bullets[i].owningshipid < 32)
                                                                                                                     {
                                                                                                                                     ships[bullets[i].owningshipid].hits++;
                                                                                                                     }
                                                                                                                     // if we are a tractor beam then do x
                                                                                                                     if (bullets[i].istractor)
                                                                                                                     {
                                                                                                                                     // slows the target down considerably......

                                                                                                                                     float svel = (float) Math.sqrt(ships[j].vx * ships[j].vx + ships[j].vy * ships[j].vy + ships[j].vz * ships[j].vz);

                                                                                                                                     if (svel > ships[j].minvel * 0.5f && ships[j].minvel > 0)
                                                                                                                                     {
                                                                                                                                                     // reduce our velocity to our minvel *
                                                                                                                                                     // 0.5f;

                                                                                                                                                     ships[j].vx *= ships[j].minvel * 0.5f / svel;
                                                                                                                                                     ships[j].vy *= ships[j].minvel * 0.5f / svel;
                                                                                                                                                     ships[j].vz *= ships[j].minvel * 0.5f / svel;

                                                                                                                                     }
                                                                                                                                     if (ships[j].minvel <= 0)
                                                                                                                                     {
                                                                                                                                                     // error!
                                                                                                                                                     Log.i(TAG, "min vel not set right... for ship type:" + ships[j].shiptype + " on team:" + ships[j].team);
                                                                                                                                     }

                                                                                                                     }
                                                                                                                     // if we are a disrupter do y.....
                                                                                                                     if (bullets[i].isdisruptor)
                                                                                                                     {
                                                                                                                                     // affects enemy power systems.....
                                                                                                                                     // basically drains energy....
                                                                                                                                     ships[j].powerlevel -= 1;
                                                                                                                                     if (ships[j].powerlevel < 0)
                                                                                                                                     {
                                                                                                                                                     ships[j].powerlevel = 0;
                                                                                                                                     }
                                                                                                                     }
                                                                                                     }
                                                                                                     continue;
                                                                                     }
                                                                                     if (ships[j] != null)
                                                                                     {
                                                                                                     if (ships[j].hp > 0)
                                                                                                     {
                                                                                                                     if (ships[j].team == bullets[i].owningshipteam)
                                                                                                                     {
                                                                                                                                     continue;
                                                                                                                     }
                                                                                                                     float dist = (float) Math.sqrt((bullets[i].x - ships[j].x) * (bullets[i].x - ships[j].x) + (bullets[i].y - ships[j].y) * (bullets[i].y - ships[j].y) + (bullets[i].z - ships[j].z) * (bullets[i].z - ships[j].z));
                                                                                                                     if (dist < ships[j].xr)
                                                                                                                     {
                                                                                                                                     // ship has been hit ......

                                                                                                                                     // check the owner.....
                                                                                                                                     if (bullets[i].owningshipid >= 0 && bullets[i].owningshipid < 32)
                                                                                                                                     {
                                                                                                                                                     ships[bullets[i].owningshipid].hits++;
                                                                                                                                     }

                                                                                                                                     ships[j].hp -= bullets[i].damageonhit;
                                                                                                                                     if (!bullets[i].isbeam)
                                                                                                                                     {
                                                                                                                                                     bullets[i].lifetime = 0;
                                                                                                                                     }
                                                                                                                                     if (ships[j].hp > 0)
                                                                                                                                     {
                                                                                                                                                     if (bullets[i].ismissile)
                                                                                                                                                     {
                                                                                                                                                                     Log.i(TAG, "Ship Damaged by MISSILE!");
                                                                                                                                                     }
                                                                                                                                                     else
                                                                                                                                                     {
                                                                                                                                                                     if (ships[bullets[i].owningshipid].shiptype < 3)
                                                                                                                                                                     {
                                                                                                                                                                                     Log.i(TAG, "Ship Damaged by FLAK!");
                                                                                                                                                                     }
                                                                                                                                                                     else
                                                                                                                                                                     {
                                                                                                                                                                                     // Log.i(TAG,"Ship Damaged by LASER!");
                                                                                                                                                                     }
                                                                                                                                                     }
                                                                                                                                                     // if we are a tractor beam then do x
                                                                                                                                                     if (bullets[i].istractor)
                                                                                                                                                     {
                                                                                                                                                                     // slows the target down
                                                                                                                                                                     // considerably......

                                                                                                                                                                     float svel = (float) Math.sqrt(ships[j].vx * ships[j].vx + ships[j].vy * ships[j].vy + ships[j].vz * ships[j].vz);

                                                                                                                                                                     if (svel > ships[j].minvel * 0.5f && ships[j].minvel > 0)
                                                                                                                                                                     {
                                                                                                                                                                                     // reduce our velocity to our
                                                                                                                                                                                     // minvel * 0.5f;

                                                                                                                                                                                     ships[j].vx *= ships[j].minvel * 0.5f / svel;
                                                                                                                                                                                     ships[j].vy *= ships[j].minvel * 0.5f / svel;
                                                                                                                                                                                     ships[j].vz *= ships[j].minvel * 0.5f / svel;

                                                                                                                                                                     }
                                                                                                                                                                     if (ships[j].minvel <= 0)
                                                                                                                                                                     {
                                                                                                                                                                                     // error!
                                                                                                                                                                                     Log.i(TAG, "min vel not set right... for ship type:" + ships[j].shiptype + " on team:" + ships[j].team);
                                                                                                                                                                     }

                                                                                                                                                     }
                                                                                                                                                     // if we are a disrupter do y.....
                                                                                                                                                     if (bullets[i].isdisruptor)
                                                                                                                                                     {
                                                                                                                                                                     // affects enemy power systems.....
                                                                                                                                                                     // basically drains energy....
                                                                                                                                                                     ships[j].powerlevel -= 1;
                                                                                                                                                                     if (ships[j].powerlevel < 0)
                                                                                                                                                                     {
                                                                                                                                                                                     ships[j].powerlevel = 0;
                                                                                                                                                                     }
                                                                                                                                                     }// projectile weapon so you do get
                                                                                                                                                         // knocked around a little bit by
                                                                                                                                                         // the
                                                                                                                                                         // impact
                                                                                                                                                         // if you are a little ship....

                                                                                                                                     }
                                                                                                                                     else
                                                                                                                                     {
                                                                                                                                                     Log.i(TAG, "Ship Destroyed!:" + j + " Ship Type:" + ships[j].shiptype + " Ship Size:" + ships[j].sizetype + " Ship full Hp:" + ships[j].fullhp);
                                                                                                                                                     if (bullets[i].owningshipid >= 0 && bullets[i].owningshipid < 32)
                                                                                                                                                     {
                                                                                                                                                                     ships[bullets[i].owningshipid].kills++;
                                                                                                                                                                     ships[bullets[i].owningshipid].hits += 3; // more
                                                                                                                                                                                                                                                                                                                                             // points
                                                                                                                                                                                                                                                                                                                                             // for
                                                                                                                                                                                                                                                                                                                                             // landing
                                                                                                                                                                                                                                                                                                                                             // the
                                                                                                                                                                                                                                                                                                                                             // final
                                                                                                                                                                                                                                                                                                                                             // blow...
                                                                                                                                                     }
                                                                                                                                     }
                                                                                                                                     break;
                                                                                                                     }
                                                                                                     }
                                                                                     }
                                                                     }
                                                                     if (bullets[i].lifetime <= 0)
                                                                     {
                                                                                     // bullets[i]=null;
                                                                     }
                                                     }

                                                     if (team0count == 0 || team1count == 0)
                                                     {
                                                                     gameover = true;
                                                     }
                                                     int frame = currentframe;
                                                     maxi = 32;
                                                     keyframe[frame] = new KeyFrame();
                                                     keyframe[frame].shipids = new byte[maxi];
                                                     keyframe[frame].shipteam = new byte[maxi];
                                                     keyframe[frame].shiptypes = new byte[maxi];
                                                     keyframe[frame].shipdata = new KeyFrameData[maxi];
                                                     for (int i = 0; i < maxi; i++)
                                                     {

                                                                     keyframe[frame].shipids[i] = (byte) i;
                                                                     keyframe[frame].shipteam[i] = (byte) ships[i].team;
                                                                     keyframe[frame].shiptypes[i] = (byte) ships[i].shiptype;
                                                                     keyframe[frame].shipdata[i] = new KeyFrameData();
                                                                     keyframe[frame].shipdata[i].x = ships[i].x;
                                                                     keyframe[frame].shipdata[i].y = ships[i].y;
                                                                     keyframe[frame].shipdata[i].z = ships[i].z;
                                                                     keyframe[frame].shipdata[i].firingframe = new byte[ships[i].hardpoints.length];

                                                                     boolean isfiring = false;
                                                                     for (int kk = 0; kk < ships[i].hardpoints.length; kk++)
                                                                     {
                                                                                     if (ships[i].hardpoints[kk].weapon.fireframe == frame)
                                                                                     {
                                                                                                     if (ships[i].hardpoints[kk].weapon.targetshipid != -1)
                                                                                                     {
                                                                                                                     keyframe[frame].shipdata[i].firingframe[kk] = (byte) (ships[i].hardpoints[kk].weapon.targetshipid + 1);
                                                                                                     }
                                                                                                     else
                                                                                                     {
                                                                                                                     keyframe[frame].shipdata[i].firingframe[kk] = 33; // means
                                                                                                                                                                                                                                                                                                                             // shoot
                                                                                                                                                                                                                                                                                                                             // without
                                                                                                                                                                                                                                                                                                                             // a
                                                                                                                                                                                                                                                                                                                             // target
                                                                                                                                                                                                                                                                                                                             // -
                                                                                                                                                                                                                                                                                                                             // unlikely...
                                                                                                     }
                                                                                                     // also store the hard point's bullet
                                                                                                     // direction......
                                                                                                     if (!isfiring)
                                                                                                     {
                                                                                                                     keyframe[frame].shipdata[i].fx = new float[ships[i].hardpoints.length];
                                                                                                                     keyframe[frame].shipdata[i].fy = new float[ships[i].hardpoints.length];
                                                                                                                     keyframe[frame].shipdata[i].fz = new float[ships[i].hardpoints.length];

                                                                                                                     keyframe[frame].shipdata[i].fux = new float[ships[i].hardpoints.length];
                                                                                                                     keyframe[frame].shipdata[i].fuy = new float[ships[i].hardpoints.length];
                                                                                                                     keyframe[frame].shipdata[i].fuz = new float[ships[i].hardpoints.length];

                                                                                                     }
                                                                                                     keyframe[frame].shipdata[i].fx[kk] = ships[i].hardpoints[kk].fx;
                                                                                                     keyframe[frame].shipdata[i].fy[kk] = ships[i].hardpoints[kk].fy;
                                                                                                     keyframe[frame].shipdata[i].fz[kk] = ships[i].hardpoints[kk].fz;
                                                                                                     keyframe[frame].shipdata[i].fux[kk] = ships[i].hardpoints[kk].fux;
                                                                                                     keyframe[frame].shipdata[i].fuy[kk] = ships[i].hardpoints[kk].fuy;
                                                                                                     keyframe[frame].shipdata[i].fuz[kk] = ships[i].hardpoints[kk].fuz;
                                                                                                     isfiring = true;
                                                                                     }
                                                                                     else
                                                                                     {
                                                                                                     keyframe[frame].shipdata[i].firingframe[kk] = 0; // means
                                                                                                                                                                                                                                                                                                         // don't
                                                                                                                                                                                                                                                                                                         // shoot.
                                                                                     }
                                                                     }
                                                                     keyframe[frame].shipdata[i].hp = (short) ships[i].hp;
                                                     }

                                                     currentframe++;
                                     }
                                     if (!gameover)
                                     {
                                                     // the attack has failed...the attackers will flee the battle at
                                                     // this point...general retreat.....
                                                     // retreating = true;
                                                     // startretreat = currentframe;
                                     }
                                     Log.i(TAG, "Team1Count:" + team1count + " team0Count:" + team0count);
                                     int finalframe = currentframe + 250;
                                     while (currentframe < finalframe) // need to work on this.....
                                     {
                                                     int frame = currentframe;
                                                     maxi = 32;
                                                     keyframe[frame] = new KeyFrame();
                                                     keyframe[frame].shipids = new byte[maxi];
                                                     keyframe[frame].shipteam = new byte[maxi];
                                                     keyframe[frame].shiptypes = new byte[maxi];
                                                     keyframe[frame].shipdata = new KeyFrameData[maxi];
                                                     for (int i = 0; i < maxi; i++)
                                                     {
                                                                     keyframe[frame].shipids[i] = (byte) i;
                                                                     keyframe[frame].shipteam[i] = (byte) ships[i].team;
                                                                     keyframe[frame].shiptypes[i] = (byte) ships[i].shiptype;
                                                                     keyframe[frame].shipdata[i] = new KeyFrameData();
                                                                     keyframe[frame].shipdata[i].x = ships[i].x;
                                                                     keyframe[frame].shipdata[i].y = ships[i].y;
                                                                     keyframe[frame].shipdata[i].z = ships[i].z;
                                                                     keyframe[frame].shipdata[i].firingframe = new byte[ships[i].hardpoints.length];
                                                                     for (int kk = 0; kk < ships[i].hardpoints.length; kk++)
                                                                     {
                                                                                     if (ships[i].hardpoints[kk].weapon.fireframe == frame)
                                                                                     {
                                                                                                     keyframe[frame].shipdata[i].firingframe[kk] = 1;
                                                                                     }
                                                                                     else
                                                                                     {
                                                                                                     keyframe[frame].shipdata[i].firingframe[kk] = 0;
                                                                                     }
                                                                     }
                                                                     keyframe[frame].shipdata[i].hp = (short) ships[i].hp;
                                                     }

                                                     currentframe++;
                                     }
                                     // successful...now write the frame data......
                                     int maxframes = currentframe;

                                     sendprogressupdate(0.75f);

                                     Log.i(TAG, "Final frame:" + currentframe);

                                     Log.i(TIMERTAG, "Camera Gen Time:" + System.currentTimeMillis());

                                     gencamera(maxframes);

                                     sendprogressupdate(0.95f);

                                     Log.i(TIMERTAG, "Time to create outputbuffer:start:" + System.currentTimeMillis());

                                     int size = 0;
                                     size += 4; // maxframes...
                                     for (int i = 0; i < maxframes; i++)
                                     {
                                                     size += keyframe[i].getsizeinbytes();
                                     }
                                     byte[] txtbytes = input.getBytes();

                                     byte[] outputbuffer = new byte[size + txtbytes.length + 4 + maxframes * 6 * 4];

                                     byte[] bytes = ByteBuffer.allocate(4).putInt(maxframes).order(ByteOrder.BIG_ENDIAN).array();
                                     for (int i = 0; i < 4; i++)
                                     {
                                                     outputbuffer[i] = bytes[i];
                                     }
                                     int offset = 4;
                                     for (int i = 0; i < maxframes; i++)
                                     {
                                                     offset = keyframe[i].writetobuffer(outputbuffer, offset);
                                     }
                                     bytes = ByteBuffer.allocate(4).putInt(txtbytes.length).order(ByteOrder.BIG_ENDIAN).array();
                                     for (int i = 0; i < 4; i++)
                                     {
                                                     outputbuffer[offset] = bytes[i];
                                                     offset++;
                                     }

                                     for (int i = 0; i < txtbytes.length; i++)
                                     {
                                                     outputbuffer[offset] = txtbytes[i];
                                                     offset++;
                                     }

                                     // put in camerakeyframes as well....

                                     for (int i = 0; i < maxframes; i++)
                                     {
                                                     if (currentframe % 100 == 0)
                                                     {
                                                                     // Intent broadcastintent = new
                                                                     // Intent("com.lloydm.geosword.activities.LaunchAttackActivity.progress");
                                                                     // broadcastintent.putExtra("frame", currentframe);
                                                                     // ctx.sendBroadcast(broadcastintent);
                                                     }

                                                     bytes = ByteBuffer.allocate(4).putFloat(cameraframe[i].x).order(ByteOrder.BIG_ENDIAN).array();
                                                     for (int j = 0; j < 4; j++)
                                                     {
                                                                     outputbuffer[offset] = bytes[j];
                                                                     offset++;
                                                     }
                                                     bytes = ByteBuffer.allocate(4).putFloat(cameraframe[i].y).order(ByteOrder.BIG_ENDIAN).array();
                                                     for (int j = 0; j < 4; j++)
                                                     {
                                                                     outputbuffer[offset] = bytes[j];
                                                                     offset++;
                                                     }
                                                     bytes = ByteBuffer.allocate(4).putFloat(cameraframe[i].z).order(ByteOrder.BIG_ENDIAN).array();
                                                     for (int j = 0; j < 4; j++)
                                                     {
                                                                     outputbuffer[offset] = bytes[j];
                                                                     offset++;
                                                     }
                                                     bytes = ByteBuffer.allocate(4).putFloat(cameraframe[i].lookx).order(ByteOrder.BIG_ENDIAN).array();
                                                     for (int j = 0; j < 4; j++)
                                                     {
                                                                     outputbuffer[offset] = bytes[j];
                                                                     offset++;
                                                     }
                                                     bytes = ByteBuffer.allocate(4).putFloat(cameraframe[i].looky).order(ByteOrder.BIG_ENDIAN).array();
                                                     for (int j = 0; j < 4; j++)
                                                     {
                                                                     outputbuffer[offset] = bytes[j];
                                                                     offset++;
                                                     }
                                                     bytes = ByteBuffer.allocate(4).putFloat(cameraframe[i].lookz).order(ByteOrder.BIG_ENDIAN).array();
                                                     for (int j = 0; j < 4; j++)
                                                     {
                                                                     outputbuffer[offset] = bytes[j];
                                                                     offset++;
                                                     }
                                     }

                                     // Deflater deflater = new Deflater();
                                     // deflater.setLevel(Deflater.BEST_COMPRESSION);
                                     // deflater.setInput(outputbuffer);
                                     Log.i(TIMERTAG, "Time to create outputbuffer:end:" + System.currentTimeMillis());
                                     try
                                     {
                                                     Log.i(TIMERTAG, "Time to save outputstream:start:" + System.currentTimeMillis());
                                                     FileOutputStream outputstream = null;

                                                     // WARNING MUST MAKE SURE EXTERNAL STORAGE IS WRITEABLE
                                                     // OTHERWISE USE INTERNAL DATA FOLDER
                                                     // AND COPY OVER EXISTING FILES......

                                                     // outputpath = Environment.getExternalStorageDirectory().getAbsolutePath();
                                                     if (!outputpath.endsWith("/"))
                                                     {
                                                                     outputpath = outputpath + "/";
                                                     }
                                                     outputpath = outputpath + Config.REPLAYFOLDER;

                                                     File dir = new File(outputpath);
                                                     if (!dir.exists())
                                                     {
                                                                     dir.mkdirs();
                                                     }
                                                     String rname = Long.toString(System.currentTimeMillis());
                                                     rname = rname.substring(0, rname.length());
                                                     if (UploadService.TESTBATTLES)
                                                     {
                                                                     rname = "_TST_" + Long.toString(System.currentTimeMillis());
                                                     }
                                                     outputstream = null;

                                                     BufferedOutputStream bos = null;
                                                     try
                                                     {
                                                                     outputstream = new FileOutputStream(outputpath + Config.REPLAYPREFIX + rname + ".EP4");

                                                                     try
                                                                     {
                                                                                     bos = new BufferedOutputStream(outputstream, 16 * 1024);
                                                                                     bos.write(outputbuffer);
                                                                                     bos.flush();

                                                                     }
                                                                     catch (IOException e)
                                                                     {
                                                                                     // TODO Auto-generated catch block
                                                                                     e.printStackTrace();
                                                                     }

                                                     }
                                                     catch (FileNotFoundException e)
                                                     {
                                                                     // TODO Auto-generated catch block
                                                                     e.printStackTrace();
                                                     }
                                                     finally
                                                     {
                                                                     try
                                                                     {
                                                                                     /*
                                                                                         * try { serout.close(); } catch(Exception ffge) { Log.e(TAG,"error closing serialised file"); }
                                                                                         *
/
                                                                                     try
                                                                                     {
                                                                                                     bos.close();
                                                                                     }
                                                                                     catch (Exception e)
                                                                                     {
                                                                                                     Log.e(TAG, "Error closing bos");
                                                                                     }
                                                                                     outputstream.close();
                                                                                     // if we got here then we were successful!
                                                                                     battlefile = outputpath + Config.REPLAYPREFIX + rname + ".EP4";
                                                                                     Log.i(TAG, "TIME:(SUCCESSFULLY WRITTEN BATTLEFILE):" + System.currentTimeMillis());
                                                                                     Log.i(TIMERTAG, "Time to save outputstream:end:" + System.currentTimeMillis());

                                                                                     // now we have to also write the battleresult out.......

                                                                                     // this will take the form of:

                                                                                     // which team won.
                                                                                     // the attacker's ships that have <=0 hp.....as a
                                                                                     // list...
                                                                                     // the number of kills each ship caused.....

                                                                                     int winningteam = ateam;
                                                                                     for (int i = 0; i < 32; i++)
                                                                                     {
                                                                                                     if (ships[i].hp > 0 && ships[i].team == dteam)
                                                                                                     {
                                                                                                                     winningteam = dteam;
                                                                                                     }
                                                                                     }
                                                                                     if (winningteam == ateam)
                                                                                     {
                                                                                                     // make sure we actually have some ships left....
                                                                                                     winningteam = dteam;
                                                                                                     for (int i = 0; i < 32; i++)
                                                                                                     {
                                                                                                                     if (ships[i].hp > 0 && ships[i].team == ateam)
                                                                                                                     {
                                                                                                                                     winningteam = ateam;
                                                                                                                     }
                                                                                                     }
                                                                                     }
                                                                                     if (islocal)
                                                                                     {
                                                                                                     SharedPreferences sp = this.srvc.getSharedPreferences("com.lloydm.geoswordlite.common.stdgameprefs", Activity.MODE_PRIVATE);
                                                                                                     if (winningteam == ateam)
                                                                                                     {
                                                                                                                     // shake up the AI player's AI settings considerably....
                                                                                                                     setlocalai(sp, true);
                                                                                                     }
                                                                                                     else
                                                                                                     {
                                                                                                                     // shake up the AI player's AI settings just a little.....
                                                                                                                     setlocalai(sp, false);
                                                                                                     }
                                                                                     }
                                                                                     // so winning team is known.....

                                                                                     // send back a string of 160 characters with either a 1
                                                                                     // or 0 indicating if the ship is alive or dead.....
                                                                                     // plus the number of kills....
                                                                                     // as well as the team number before it....

                                                                                     String hstatus = Integer.toString(winningteam);
                                                                                     for (int i = 0; i < 32; i++)
                                                                                     {
                                                                                                     hstatus = hstatus + Integer.toString(ships[i].team); // 1
                                                                                                                                                                                                                                                                                                                         // -
                                                                                                                                                                                                                                                                                                                         // 8
                                                                                                     hstatus = hstatus + Integer.toString(ships[i].shiptype); // 1
                                                                                                                                                                                                                                                                                                                                         // -
                                                                                                                                                                                                                                                                                                                                         // 4
                                                                                                     if (ships[i].kills < 10)
                                                                                                     {
                                                                                                                     hstatus = hstatus + "0" + Integer.toString(ships[i].kills);
                                                                                                     }
                                                                                                     else
                                                                                                     {
                                                                                                                     hstatus = hstatus + Integer.toString(ships[i].kills);
                                                                                                     }
                                                                                                     if (ships[i].hp > 0)
                                                                                                     {
                                                                                                                     hstatus = hstatus + "1";
                                                                                                     }
                                                                                                     else
                                                                                                     {
                                                                                                                     hstatus = hstatus + "0";
                                                                                                     }
                                                                                     }
                                                                                     battleresult = hstatus;

                                                                                     sendprogressupdate(0.99f);
                                                                     }
                                                                     catch (Exception e)
                                                                     {
                                                                                     Log.e(TAG, "Problem writing file");
                                                                     }
                                                     }
                                     }
                                     catch (Exception fffg)
                                     {
                                                     Log.i(TAG, "Exception creating folder");
                                     }
                     }
                     else
                     {
                                     Log.i(TAG, "Dodgy data in input");
                     }
     }

     public String getBattleResult()
     {
                     if (battleresult != null)
                     {
                                     return battleresult;
                     }
                     else
                     {
                                     return "";
                     }
     }

     public String getBattleFile()
     {
                     if (battlefile != null)
                     {
                                     return battlefile;
                     }
                     else
                     {
                                     return "";
                     }
     }

     private boolean targetcalc(int i, int k)
     {
                     float gunposx, gunposy, gunposz, gunposdx, gunposdy, gunposdz;

                     // position of gun muzzle...
                     SimpleVector vec = new SimpleVector(ships[i].hardpoints[k].x, ships[i].hardpoints[k].y, ships[i].hardpoints[k].z);
                     SimpleVector vec2 = new SimpleVector(ships[i].vx, ships[i].vy, ships[i].vz);
                     vec2 = vec2.normalize(vec2);
                     vec.rotate(vec2.getRotationMatrix());

                     gunposx = ships[i].x + vec.x;
                     gunposy = ships[i].y + vec.y;
                     gunposz = ships[i].z + vec.z;

                     vec = new SimpleVector(ships[i].hardpoints[k].dx, ships[i].hardpoints[k].dy, ships[i].hardpoints[k].dz);

                     vec.rotate(vec2.getRotationMatrix());

                     gunposdx = vec.x;
                     gunposdy = vec.y;
                     gunposdz = vec.z;

                     float glen = (float) Math.sqrt(gunposdx * gunposdx + gunposdy * gunposdy + gunposdz * gunposdz);
                     if (glen > 0)
                     {
                                     // if(ships[i].hardpoints[k].weapon.isbeam==true)
                                     // {
                                     // Log.i(TAG,"BEAM ON THIS SHIP! + 10");
                                     // }

                                     gunposdx /= glen;
                                     gunposdy /= glen;
                                     gunposdz /= glen;

                                     for (int j = 0; j < 32; j++)
                                     {
                                                     if (ships[i].team != ships[j].team && ships[j].hp > 0)
                                                     {
                                                                     // rel velocities are fine...
                                                                     float relvelx = ships[j].vx - ships[i].vx;
                                                                     float relvely = ships[j].vy - ships[i].vy;
                                                                     float relvelz = ships[j].vz - ships[i].vz;

                                                                     // rel positions are not quite correct....
                                                                     // it is actually the position of the hardpoint....
                                                                     // transformed from object to world space....
                                                                     // and then not only that...we have to make sure the enemy
                                                                     // ship
                                                                     //

                                                                     float relposx = ships[j].x - gunposx;
                                                                     float relposy = ships[j].y - gunposy;
                                                                     float relposz = ships[j].z - gunposz;

                                                                     // dot product must be within fire arc of weapon......
                                                                     float relposdist = (float) Math.sqrt(relposx * relposx + relposy * relposy + relposz * relposz);
                                                                     if (relposdist <= 0)
                                                                     {
                                                                                     return false;
                                                                     }
                                                                     float gundot = (gunposdx * relposx + gunposdy * relposy + gunposdz * relposz) / relposdist;

                                                                     if (gundot < ships[i].hardpoints[k].mintargetingdot)
                                                                     {
                                                                                     return false;
                                                                     }

                                                                     if (ships[i].hardpoints[k].weapon.isbeam)
                                                                     {
                                                                                     // Log.i(TAG,"BEAM LOOKING GOOD!");
                                                                                     if (ships[j].shiptype < 3 || (ships[i].hardpoints[k].weapon.weapontype == 5))
                                                                                     {

                                                                                                     // tractor beams go for anybody at all! other beams
                                                                                                     // only go for big ships....

                                                                                                     Log.i(TAG, "BEAM POSSIBLE ENEMY:" + relposdist);
                                                                                                     if (relposdist < 2250) // BEAMS ARE REALLY LONG
                                                                                                                                                                                                 // RANGED NOW!!!!!
                                                                                                     {
                                                                                                                     // Log.i(TAG,"BEAM LOOKING VERY GOOD");
                                                                                                                     ships[i].hardpoints[k].weapon.targetshipid = j;
                                                                                                                     return true;
                                                                                                                     // yep!
                                                                                                     }
                                                                                     }
                                                                                     continue;
                                                                     }

                                                                     //special case for when directly behind.......
                                                                     float relveldist = (float) Math.sqrt(relvelx*relvelx + relvely*relvely + relvelz*relvelz);
                                                                     if(relveldist<0.1f)
                                                                     {
                                                                                     //so very little difference in our velocities.....
                                                                                     if(gundot>0.925f)
                                                                                     {
                                                                                                     //lined up behind......
                                                                                                     if(relposdist<550f)
                                                                                                     {
                                                                                                                     return true;
                                                                                                     }
                                                                                     }
                                                                     }

                                                                     float DxDx = relposx * relposx;
                                                                     float DyDy = relposy * relposy;
                                                                     float DzDz = relposz * relposz;

                                                                     float DxVDx = relposx * relvelx;
                                                                     float DyVDy = relposy * relvely;
                                                                     float DzVDz = relposz * relvelz;

                                                                     float VDxVDx = relvelx * relvelx;
                                                                     float VDyVDy = relvely * relvely;
                                                                     float VDzVDz = relvelz * relvelz;

                                                                     float L = ships[i].hardpoints[k].weapon.muzzlevel;
                                                                     float LL = L * L;
                                                                     float T = -1;
                                                                     if (LL != VDxVDx + VDyVDy + VDzVDz)
                                                                     {
                                                                                     float BB = (DxVDx + DyVDy + DzVDz) * (DxVDx + DyVDy + DzVDz);
                                                                                     float AC = (LL - (VDxVDx + VDyVDy + VDzVDz)) * (DxDx + DyDy + DzDz);
                                                                                     if (BB + AC >= 0)
                                                                                     {
                                                                                                     float sqrBBAC = (float) Math.sqrt(BB + AC);
                                                                                                     float T1 = (DxVDx + DyVDy + DzVDz + sqrBBAC) / (LL - (VDxVDx + VDyVDy + VDzVDz));
                                                                                                     float T2 = (DxVDx + DyVDy + DzVDz - sqrBBAC) / (LL - (VDxVDx + VDyVDy + VDzVDz));
                                                                                                     if (T1 < 0)
                                                                                                     {
                                                                                                                     if (T2 > 0)
                                                                                                                     {
                                                                                                                                     T = T2;
                                                                                                                     }
                                                                                                     }
                                                                                                     if (T2 < 0)
                                                                                                     {
                                                                                                                     if (T1 > 0)
                                                                                                                     {
                                                                                                                                     T = T1;
                                                                                                                     }
                                                                                                     }
                                                                                                     if (T1 > 0 && T2 > 0)
                                                                                                     {
                                                                                                                     if (T1 < T2)
                                                                                                                     {
                                                                                                                                     T = T1;
                                                                                                                     }
                                                                                                                     else
                                                                                                                     {
                                                                                                                                     T = T2;
                                                                                                                     }
                                                                                                     }

                                                                                     }
                                                                     }
                                                                     else
                                                                     {
                                                                                     return false;
                                                                     }

                                                                     if (T > 0 && T < ships[i].hardpoints[k].weapon.baselifetime)
                                                                     {
                                                                                     float bulvx = (relposx + (relvelx * T)) / T;
                                                                                     float bulvy = (relposy + (relvely * T)) / T;
                                                                                     float bulvz = (relposz + (relvelz * T)) / T;
                                                                                     // float bulT = T;
                                                                                     float bulv = (float) Math.sqrt(bulvx * bulvx + bulvy * bulvy + bulvz * bulvz);
                                                                                     if (bulv > ships[i].hardpoints[k].weapon.muzzlevel + 1.0)
                                                                                     {
                                                                                                     return false; // should never get here....
                                                                                     }
                                                                                     if (bulv <= 0)
                                                                                     {
                                                                                                     return false; // should never get here either....
                                                                                     }
                                                                                     ships[i].hardpoints[k].weapon.targetshipid = j;
                                                                                     return true;
                                                                     }
                                                     }
                                     }
                     }
                     return false;
     }

     private boolean readfrominput(String input)
     {
                     // initialise all ships etc....positions, weapon loadouts etc etc...
                     try
                     {
                                     String[] shipstr = input.split(":");
                                     // int team0 = -1;
                                     int team1 = -1;
                                     if (shipstr != null)
                                     {
                                                     if (shipstr.length > 0)
                                                     {
                                                                     Log.i(TAG, "Shipstr length:" + shipstr.length);
                                                                     int size = shipstr.length;

                                                                     if (size > 32)
                                                                     {
                                                                                     size = 32;
                                                                     }
                                                                     for (int s = 0; s < size; s++)
                                                                     {
                                                                                     // Log.i(TAG,"Ship S:"+s);
                                                                                     Ship ship = new Ship();
                                                                                     String[] fields = shipstr[s].split("\\|");
                                                                                     if (fields != null)
                                                                                     {
                                                                                                     // Log.i(TAG,"Ship fields length:"+fields.length);
                                                                                                     // for(int i=0;i<fields.length;i++)
                                                                                                     // {
                                                                                                     ship.id = Integer.decode(fields[0]);
                                                                                                     ship.shiptype = Integer.decode(fields[1]);
                                                                                                     ship.hp = (int) Float.parseFloat(fields[2]);
                                                                                                     if (ship.hp <= 0)
                                                                                                     {
                                                                                                                     ship.inithp = 0;
                                                                                                     }
                                                                                                     else
                                                                                                     {
                                                                                                                     ship.inithp = 1;
                                                                                                     }
                                                                                                     ship.team = Integer.decode(fields[3]);
                                                                                                     if (s < 16)
                                                                                                     {
                                                                                                                     ateam = ship.team;
                                                                                                                     // team0 = ateam;
                                                                                                     }
                                                                                                     else
                                                                                                     {
                                                                                                                     dteam = ship.team;
                                                                                                                     team1 = dteam;
                                                                                                     }
                                                                                                     /*
                                                                                                         * if(team0==-1) { team0 = ship.team; //attacker ateam = team0; } else { if(ship.team!=team0) { team1 = ship.team; dteam = team1; } }
                                                                                                         *
/
                                                                                                     ship.sizetype = Integer.decode(fields[4]);
                                                                                                     ship.maxvel = Float.parseFloat(fields[5]);
                                                                                                     ship.minvel = Float.parseFloat(fields[6]);
                                                                                                     ship.maxaccel = Float.parseFloat(fields[7]);
                                                                                                     ship.minaccel = Float.parseFloat(fields[8]);

                                                                                                     // ship.maxrotaccel = Float.parseFloat(fields[8]);
                                                                                                     // ship.minrotaccel = Float.parseFloat(fields[9]);
                                                                                                     ship.fullhp = (int) Float.parseFloat(fields[9]);
                                                                                                     // ship.hp = ship.fullhp;
                                                                                                     ship.cputime = Integer.decode(fields[12]);
                                                                                                     ship.cpuindex = Integer.decode(fields[12]);
                                                                                                     ship.powerlevel = 100;// Integer.decode(fields[13]);
                                                                                                     ship.rechargerate = 2;// Integer.decode(fields[14]);
                                                                                                     int offset = 13;
                                                                                                     // only used for test battles.....

                                                                                                     // this should just work......
                                                                                                     final long tttime = (System.currentTimeMillis() / 3000);
                                                                                                     final int tti = (int) Math.abs(tttime % 65535);
                                                                                                     for (int j = 0; j < 16; j++)
                                                                                                     {
                                                                                                                     ship.aislidervalues[j] = Integer.decode(fields[offset]);

                                                                                                                     // if testbattles then play around with these values a little.....

                                                                                                                     if (UploadService.TESTBATTLES)
                                                                                                                     {
                                                                                                                                     ship.aislidervalues[j] = (s + j + tti) % 10;
                                                                                                                     }
                                                                                                                     offset++;
                                                                                                     }

                                                                                                     int numh = Integer.decode(fields[offset]);
                                                                                                     offset++;
                                                                                                     ship.hardpoints = new HardPoint[numh];
                                                                                                     for (int j = 0; j < numh; j++)
                                                                                                     {
                                                                                                                     ship.hardpoints[j] = new HardPoint();
                                                                                                                     ship.hardpoints[j].x = Float.parseFloat(fields[offset]);
                                                                                                                     ship.hardpoints[j].y = Float.parseFloat(fields[offset + 1]);
                                                                                                                     ship.hardpoints[j].z = Float.parseFloat(fields[offset + 2]);
                                                                                                                     ship.hardpoints[j].dx = Float.parseFloat(fields[offset + 3]);
                                                                                                                     ship.hardpoints[j].dy = Float.parseFloat(fields[offset + 4]);
                                                                                                                     ship.hardpoints[j].dz = Float.parseFloat(fields[offset + 5]);
                                                                                                                     ship.hardpoints[j].hp = Integer.decode(fields[offset + 6]);
                                                                                                                     if (ship.hardpoints[j].hp <= 0)
                                                                                                                     {
                                                                                                                                     Log.i(TAG, "Error hp of hard point is zero!! FIXING");
                                                                                                                                     ship.hardpoints[j].hp = 100;
                                                                                                                     }
                                                                                                                     ship.hardpoints[j].mintargetingdot = Float.parseFloat(fields[offset + 7]);
                                                                                                                     ship.hardpoints[j].weapon = new Weapon();
                                                                                                                     ship.hardpoints[j].weapon.damageperhit = Float.parseFloat(fields[offset + 8]);// Integer.decode(fields[offset+8]);

                                                                                                                     if (ship.hardpoints[j].weapon.damageperhit < 1)
                                                                                                                     {
                                                                                                                                     ship.hardpoints[j].weapon.damageperhit = 1;
                                                                                                                     }
                                                                                                                     ship.hardpoints[j].weapon.cooldowntimerequired = Integer.decode(fields[offset + 9]);
                                                                                                                     ship.hardpoints[j].weapon.cooldownindex = 0;// Integer.decode(fields[offset+10]);
                                                                                                                     ship.hardpoints[j].weapon.ammolimit = Integer.decode(fields[offset + 11]);
                                                                                                                     ship.hardpoints[j].weapon.ammoleft = Integer.decode(fields[offset + 12]);
                                                                                                                     ship.hardpoints[j].weapon.powerdrain = Integer.decode(fields[offset + 13]);
                                                                                                                     ship.hardpoints[j].weapon.locktimerequired = Integer.decode(fields[offset + 14]);
                                                                                                                     ship.hardpoints[j].weapon.locktimeindex = Integer.decode(fields[offset + 15]);
                                                                                                                     ship.hardpoints[j].weapon.baselifetime = Integer.decode(fields[offset + 16]);
                                                                                                                     if (Integer.decode(fields[offset + 17]) == 1)
                                                                                                                     {
                                                                                                                                     ship.hardpoints[j].weapon.isbeam = true;
                                                                                                                                     // Log.i(TAG,"Yep Beams Exist");
                                                                                                                     }
                                                                                                                     else
                                                                                                                     {
                                                                                                                                     ship.hardpoints[j].weapon.isbeam = false;
                                                                                                                     }
                                                                                                                     if (Integer.decode(fields[offset + 18]) == 1)
                                                                                                                     {
                                                                                                                                     ship.hardpoints[j].weapon.ismissile = true;
                                                                                                                     }
                                                                                                                     else
                                                                                                                     {
                                                                                                                                     ship.hardpoints[j].weapon.ismissile = false;
                                                                                                                     }
                                                                                                                     if (Integer.decode(fields[offset + 19]) == 1)
                                                                                                                     {
                                                                                                                                     ship.hardpoints[j].weapon.isguided = true;
                                                                                                                     }
                                                                                                                     else
                                                                                                                     {
                                                                                                                                     ship.hardpoints[j].weapon.isguided = false;
                                                                                                                     }
                                                                                                                     ship.hardpoints[j].weapon.guidedturnrate = Float.parseFloat(fields[offset + 20]);
                                                                                                                     if (ship.hardpoints[j].weapon.ismissile)
                                                                                                                     {
                                                                                                                                     ship.hardpoints[j].weapon.isguided = true;
                                                                                                                                     ship.hardpoints[j].weapon.guidedturnrate = 0.01f;
                                                                                                                     }
                                                                                                                     ship.hardpoints[j].weapon.muzzlevel = Float.parseFloat(fields[offset + 21]);
                                                                                                                     ship.hardpoints[j].weapon.optimumrange = Float.parseFloat(fields[offset + 22]);
                                                                                                                     ship.hardpoints[j].weapon.weapontype = Integer.decode(fields[offset + 23]);

                                                                                                                     // tractors and disruptors
                                                                                                                     if (ship.hardpoints[j].weapon.weapontype == 5)
                                                                                                                     {
                                                                                                                                     // don't do any damage...
                                                                                                                                     ship.hardpoints[j].weapon.damageperhit = 0;
                                                                                                                                     ship.hardpoints[j].weapon.isbeam = true;
                                                                                                                     }
                                                                                                                     if (ship.hardpoints[j].weapon.weapontype == 6)
                                                                                                                     {
                                                                                                                                     if (ship.hardpoints[j].weapon.baselifetime < 3)
                                                                                                                                     {
                                                                                                                                                     ship.hardpoints[j].weapon.isbeam = true;
                                                                                                                                     }
                                                                                                                                     else
                                                                                                                                     {
                                                                                                                                                     ship.hardpoints[j].weapon.isbeam = false;
                                                                                                                                     }
                                                                                                                     }

                                                                                                                     // plasma.....kind of counts as a missile....
                                                                                                                     if (ship.hardpoints[j].weapon.weapontype == 4)
                                                                                                                     {
                                                                                                                                     ship.hardpoints[j].weapon.ismissile = true;
                                                                                                                                     ship.hardpoints[j].weapon.isguided = false;
                                                                                                                     }
                                                                                                                     // swarmer missile...
                                                                                                                     if (ship.hardpoints[j].weapon.weapontype == 7)
                                                                                                                     {
                                                                                                                                     ship.hardpoints[j].weapon.ismissile = true;
                                                                                                                                     ship.hardpoints[j].weapon.isguided = true;
                                                                                                                                     ship.hardpoints[j].weapon.guidedturnrate = 0.05f; // very
                                                                                                                     }
                                                                                                                     // some fancy weapons exist.....
                                                                                                                     // missiles also do blast damage....
                                                                                                                     // missiles = 3;
                                                                                                                     // plasma = 4,
                                                                                                                     // disruptors = 5 (energy sappers/damagers)
                                                                                                                     // tractor beams = 6 (slowers etc)

                                                                                                                     offset += 24;
                                                                                                     }
                                                                                                     ship.xr = Float.parseFloat(fields[offset]);
                                                                                                     // Log.i(TAG,"Shipxr as a check:"+ship.xr);
                                                                                                     offset++;
                                                                                                     if (Integer.decode(fields[offset]) == 1)
                                                                                                     {
                                                                                                                     ship.iscap = true;

                                                                                                     }
                                                                                                     else
                                                                                                     {
                                                                                                                     ship.iscap = false;
                                                                                                     }
                                                                                                     if (ship.shiptype == 1)
                                                                                                     {
                                                                                                                     ship.iscap = false;
                                                                                                     }
                                                                                                     offset++;
                                                                                                     if (Integer.decode(fields[offset]) == 1)
                                                                                                     {
                                                                                                                     ship.isace = true;
                                                                                                     }
                                                                                                     else
                                                                                                     {
                                                                                                                     ship.isace = false;
                                                                                                     }
                                                                                                     offset++;
                                                                                                     if (fields[offset].equals("ZZ"))
                                                                                                     {
                                                                                                                     Log.i(TAG, "Data good!");
                                                                                                     }
                                                                                                     else
                                                                                                     {
                                                                                                                     Log.i(TAG, "Data bad:" + s);
                                                                                                                     return false;
                                                                                                     }
                                                                                                     if (ship.team == team1)
                                                                                                     {
                                                                                                                     // defenders...

                                                                                                                     // some kind of formation system would be lovely
                                                                                                                     // eventually!

                                                                                                                     // for now we use this....oh? :-(

                                                                                                                     ship.x = (s % 4) * 150;
                                                                                                                     ship.y = ((s - s % 4) / 4) * 150;

                                                                                                                     ship.z = -2500;
                                                                                                                     if (ship.shiptype == 4)
                                                                                                                     {
                                                                                                                                     ship.z += 300;
                                                                                                                     }
                                                                                                                     if (ship.shiptype == 1)
                                                                                                                     {
                                                                                                                                     ship.y -= -250f;
                                                                                                                     }
                                                                                                                     if (ship.shiptype > 2)
                                                                                                                     {
                                                                                                                                     ship.x -= 225f;
                                                                                                                                     ship.x *= 3f;
                                                                                                                                     if (ship.shiptype == 3)
                                                                                                                                     {
                                                                                                                                                     ship.y *= -1;
                                                                                                                                     }
                                                                                                                     }
                                                                                                                     ship.vx = 0;
                                                                                                                     ship.vy = 0;
                                                                                                                     ship.vz = (ship.minvel + ship.maxvel) * 0.5f;
                                                                                                     }
                                                                                                     else
                                                                                                     {
                                                                                                                     // attackers...
                                                                                                                     ship.x = (s % 4) * 150;
                                                                                                                     ship.y = ((s - s % 4) / 4) * 150;
                                                                                                                     ship.z = 3700;

                                                                                                                     if (ship.shiptype == 4)
                                                                                                                     {
                                                                                                                                     ship.z -= 300;
                                                                                                                     }
                                                                                                                     if (ship.shiptype == 1)
                                                                                                                     {
                                                                                                                                     ship.y -= -250f;
                                                                                                                     }
                                                                                                                     if (ship.shiptype > 2)
                                                                                                                     {
                                                                                                                                     ship.x -= 225f;
                                                                                                                                     ship.x *= 3f;
                                                                                                                                     if (ship.shiptype == 3)
                                                                                                                                     {
                                                                                                                                                     ship.y *= -1;
                                                                                                                                     }
                                                                                                                     }
                                                                                                                     ship.vx = 0;
                                                                                                                     ship.vy = 0;
                                                                                                                     ship.vz = -(ship.minvel + ship.maxvel) * 0.5f;

                                                                                                     }
                                                                                     }
                                                                                     ships[s] = ship;
                                                                     }

                                                     }
                                     }

                                     Log.i(TAG, "System got here should be okay!");

                                     return true;

                     }
                     catch (Exception e)
                     {

                                     return false;
                     }
     }

     private void gencamera(int maxframes)
     {
                     int mode = 0;
                     int fid = -1;
                     // int gid = -1;
                     int startframe = 0;
                     int tmid = -1;
                     int pktm = ateam;
                     Log.i(TAG, "ateam:" + ateam + " dteam:" + dteam);

                     for (int i = 0; i < maxframes; i++)
                     {
                                     if ((i % 300) == 0)
                                     {
                                                     sendprogressupdate(0.75f + 0.2f * ((float) i / (float) maxframes));
                                     }

                                     cameraframe[i] = new CameraKeyFrame();
                                     // initial bit here is a helper for when ships are destroyed....
                                     for (int j = 0; j < 32; j++)
                                     {
                                                     if (i > 1)
                                                     {
                                                                     if (keyframe[i].shipdata[j].hp <= 0)
                                                                     {
                                                                                     // keep moving in same direction....helps camera.....
                                                                                     keyframe[i].shipdata[j].x = keyframe[i - 2].shipdata[j].x + keyframe[i - 1].shipdata[j].x - keyframe[i - 2].shipdata[j].x;
                                                                                     keyframe[i].shipdata[j].y = keyframe[i - 2].shipdata[j].y + keyframe[i - 1].shipdata[j].y - keyframe[i - 2].shipdata[j].y;
                                                                                     keyframe[i].shipdata[j].z = keyframe[i - 2].shipdata[j].z + keyframe[i - 1].shipdata[j].z - keyframe[i - 2].shipdata[j].z;
                                                                     }
                                                     }
                                     }
                                     cameraframe[i].x = 0f;
                                     cameraframe[i].y = 0f;
                                     cameraframe[i].z = 0f;

                                     cameraframe[i].looky = 0f;
                                     cameraframe[i].lookx = 0f;
                                     cameraframe[i].lookz = 1f;

                                     if (i > 10 && i < maxframes - 10)
                                     {
                                                     // gives a bit of room to play with.....in terms of looking
                                                     // ahead or behind when setting the camera....
                                                     if (i < 750)
                                                     {
                                                                     // opening scenes....
                                                                     camwideshot(i, maxframes, dteam, 11);
                                                                     continue;
                                                     }
                                                     if (mode < 0 || mode > 3)
                                                     {
                                                                     mode = 0;
                                                     }
                                                     if ((i - 750) % 500 == 0 || (((i - 750) % 350 == 0) && mode == 3)) // wide angle is shorter duration...
                                                     {
                                                                     startframe = i;
                                                                     // pick a random mode I think instead.....new feature....spice it up a little.....
                                                                     int omode = mode;
                                                                     mode = rand.nextInt(4);
                                                                     mode %= 4;
                                                                     if (omode == mode)
                                                                     {
                                                                                     mode++;
                                                                                     mode %= 4;
                                                                     }
                                                                     // end of new addition....
                                                                     fid = -1;
                                                                     if (mode == 0)
                                                                     {
                                                                                     tmid *= -1;
                                                                     }
                                                                     if (tmid < 0)
                                                                     {
                                                                                     pktm = ateam;
                                                                     }
                                                                     else
                                                                     {
                                                                                     pktm = dteam;
                                                                     }
                                                     }

                                                     // we mainly want to do action shots.....
                                                     // default to an action shot unless there is nothing....

                                                     if (mode == 0)
                                                     {
                                                                     fid = cambigship(i, maxframes, fid, startframe, pktm);
                                                     }
                                                     if (mode == 1)
                                                     {
                                                                     fid = camlittleship(i, maxframes, fid, startframe, pktm);
                                                     }
                                                     if (mode == 3)
                                                     {
                                                                     camwideshot(i, maxframes, pktm, startframe);
                                                                     continue;
                                                     }
                                                     if (mode == 2 || fid == -1)
                                                     {
                                                                     if (fid == -1)
                                                                     {
                                                                                     mode = 2;
                                                                     }
                                                                     fid = camaction(i, maxframes, startframe, fid, pktm);
                                                     }
                                                     if (fid == -1)
                                                     {
                                                                     mode = 3;
                                                                     camwideshot(i, maxframes, pktm, startframe);
                                                                     continue;

                                                     }
                                                     else
                                                     {
                                                                     if (keyframe[i].shipdata[fid].hp <= 0)
                                                                     {
                                                                                     fid = -1;
                                                                                     cameraframe[i].x = 2 * cameraframe[i - 1].x - cameraframe[i - 2].x;
                                                                                     cameraframe[i].y = 2 * cameraframe[i - 1].y - cameraframe[i - 2].y;
                                                                                     cameraframe[i].z = 2 * cameraframe[i - 1].z - cameraframe[i - 2].z;
                                                                                     cameraframe[i].lookx = cameraframe[i - 1].lookx;
                                                                                     cameraframe[i].looky = cameraframe[i - 1].looky;
                                                                                     cameraframe[i].lookz = cameraframe[i - 1].lookz;
                                                                     }
                                                     }
                                     }
                     }
     }

     private int camlittleship(int i, int maxframes, int fid, int startframe, int pktm)
     {
                     int tscore = 0;
                     int score = 0;
                     if (fid != -1)
                     {
                                     if (keyframe[i].shipdata[fid].hp <= 0)
                                     {
                                                     // reset the fid since the ship has been destroyed - quite
                                                     // common!
                                                     if (keyframe[i - 150].shipdata[fid].hp <= 0) // or we've been
                                                                                                                                                                                                                                         // dead a little
                                                                                                                                                                                                                                         // while...
                                                     {
                                                                     fid = -1;
                                                     }
                                     }
                     }
                     if (fid == -1)
                     {
                                     // Log.i(TAG,"Little ship changing cam"+i);
                                     for (int j = 0; j < 32; j++)
                                     {
                                                     score = 0;
                                                     if (keyframe[i].shiptypes[j] > 2) // little ship
                                                     {
                                                                     // score++; //don't want to just follow any old little ship
                                                                     // - just because it is a little ship
                                                                     for (int jj = startframe + 30; jj < startframe + 250; jj++)
                                                                     {
                                                                                     if (jj < maxframes)
                                                                                     {
                                                                                                     if (keyframe[jj].shipdata[j].hp != keyframe[jj - 1].shipdata[j].hp)
                                                                                                     {
                                                                                                                     // being damaged.....
                                                                                                                     score += 3;
                                                                                                                     if (ships[j].team == pktm)
                                                                                                                     {
                                                                                                                                     score++;
                                                                                                                     }

                                                                                                     }
                                                                                                     for (int kk = 0; kk < keyframe[jj].shipdata[j].firingframe.length; kk++)
                                                                                                     {
                                                                                                                     if (keyframe[jj].shipdata[j].firingframe[kk] > 0)
                                                                                                                     {
                                                                                                                                     score += 4;
                                                                                                                                     if (ships[j].team == pktm)
                                                                                                                                     {
                                                                                                                                                     score++;
                                                                                                                                     }

                                                                                                                     }
                                                                                                     }
                                                                                     }
                                                                     }
                                                     }
                                                     if (score > tscore)
                                                     {
                                                                     tscore = score;
                                                                     fid = j;
                                                     }
                                     }
                     }
                     if (fid == -1)
                     {
                                     // Log.i(TAG,"Could not place camera frames:"+i);
                     }
                     else
                     {
                                     if ((startframe + fid) % 9 < 4)
                                     {
                                                     cameraframe[i].lookx = keyframe[i].shipdata[fid].x;
                                                     cameraframe[i].looky = keyframe[i].shipdata[fid].y;
                                                     cameraframe[i].lookz = keyframe[i].shipdata[fid].z;

                                                     // technically we always want to look just a little bit ahead of the small ship....
                                                     // that's what I've learned from a few tips on cinematography....rather than looking
                                                     // straight at the subject....look slightly ahead....obviously the camera position will affect this....

                                                     // these little guys move around 3-5 units per frame...so in 100 frames they could travel up to
                                                     // 300-500 units in world space...that's a lot...
                                                     // so we really only want to look about 30-50 units in front of them.....
                                                     // so pick a time no more than 10 frames ahead....
                                                     // we will use 7 or 8 for now....

                                                     if (i < maxframes - 10)
                                                     {
                                                                     cameraframe[i].lookx = keyframe[i + 7].shipdata[fid].x;
                                                                     cameraframe[i].looky = keyframe[i + 9].shipdata[fid].y;
                                                                     cameraframe[i].lookz = keyframe[i + 6].shipdata[fid].z;
                                                     }

                                                     int i0 = i - 100;
                                                     int i1 = i + 100;

                                                     if (i1 >= maxframes - 50)
                                                     {
                                                                     i1 = maxframes - 50;
                                                                     i0 = i1 - 50;
                                                     }

                                                     float sx = keyframe[i0].shipdata[fid].x;
                                                     float fx = keyframe[i1].shipdata[fid].x;

                                                     float sy = keyframe[i0].shipdata[fid].y;
                                                     float fy = keyframe[i1].shipdata[fid].y;

                                                     float sz = keyframe[i0].shipdata[fid].z;
                                                     float fz = keyframe[i1].shipdata[fid].z;

                                                     float vx = fx - sx;
                                                     float vy = fy - sy;
                                                     float vz = fz - sz;

                                                     float vlen = (float) Math.sqrt(vx * vx + vy * vy + vz + vz);
                                                     if (vlen > 0)
                                                     {
                                                                     vx /= vlen;
                                                                     vy /= vlen;
                                                                     vz /= vlen;
                                                     }

                                                     // find a vector perpendicular to this one....
                                                     // good old dot product again.....

                                                     // dot = a.b = ax * bx + ay * by + az * bz;
                                                     // at right angles when = 0....
                                                     // let bz = 0;
                                                     // bx =-ay and by = ax.....

                                                     float bx = vy;
                                                     float by = -vx;
                                                     float bz = 0;

                                                     float bfactor = 350f + (i - (startframe + 100)) * 0.35f;

                                                     if (i == startframe)
                                                     {
                                                                     cameraframe[i].x = keyframe[i].shipdata[fid].x + bfactor * bx;
                                                                     cameraframe[i].y = keyframe[i].shipdata[fid].y + bfactor * by;
                                                                     cameraframe[i].z = keyframe[i].shipdata[fid].z + bfactor * bz;
                                                     }
                                                     else
                                                     {
                                                                     cameraframe[i].x = cameraframe[i - 1].x + ((keyframe[i].shipdata[fid].x + bfactor * bx) - cameraframe[i - 1].x) * 0.005f;
                                                                     cameraframe[i].y = cameraframe[i - 1].y + ((keyframe[i].shipdata[fid].y + bfactor * by) - cameraframe[i - 1].y) * 0.005f;
                                                                     cameraframe[i].z = cameraframe[i - 1].z + ((keyframe[i].shipdata[fid].z + bfactor * bz) - cameraframe[i - 1].z) * 0.005f;
                                                     }
                                     }
                                     else
                                     {
                                                     if (i == startframe && i < maxframes - 10)
                                                     {

                                                                     cameraframe[i].lookx = keyframe[i + 5].shipdata[fid].x;
                                                                     cameraframe[i].looky = keyframe[i + 9].shipdata[fid].y;
                                                                     cameraframe[i].lookz = keyframe[i + 7].shipdata[fid].z;
                                                     }
                                                     else
                                                     {
                                                                     if (i < maxframes - 10)
                                                                     {
                                                                                     cameraframe[i].lookx = cameraframe[i - 1].lookx + ((keyframe[i + 5].shipdata[fid].x) - cameraframe[i - 1].x) * 0.15f;
                                                                                     cameraframe[i].looky = cameraframe[i - 1].looky + ((keyframe[i + 9].shipdata[fid].y) - cameraframe[i - 1].y) * 0.15f;
                                                                                     cameraframe[i].lookz = cameraframe[i - 1].lookz + ((keyframe[i + 7].shipdata[fid].z) - cameraframe[i - 1].z) * 0.15f;
                                                                     }
                                                     }

                                                     // pick a point.....
                                                     int ddd = (startframe + fid) % 3;
                                                     if (ddd == 0)
                                                     {
                                                                     cameraframe[i].x = keyframe[i - 20].shipdata[fid].x;
                                                                     cameraframe[i].y = keyframe[i - 20].shipdata[fid].y;
                                                                     cameraframe[i].z = keyframe[i - 20].shipdata[fid].z;

                                                     }
                                                     if (ddd == 1 && i + 25 < maxframes)
                                                     {
                                                                     cameraframe[i].x = keyframe[i + 25].shipdata[fid].x;
                                                                     cameraframe[i].y = keyframe[i + 25].shipdata[fid].y;
                                                                     cameraframe[i].z = keyframe[i + 25].shipdata[fid].z;
                                                     }
                                                     if (ddd == 1 && i + 25 >= maxframes && i < maxframes)
                                                     {
                                                                     cameraframe[i].x = keyframe[i].shipdata[fid].x + 50f;
                                                                     cameraframe[i].y = keyframe[i].shipdata[fid].y + 50f;
                                                                     cameraframe[i].z = keyframe[i].shipdata[fid].z + 250f;
                                                     }
                                                     if (ddd != 0 && ddd != 1)
                                                     {
                                                                     cameraframe[i].x = keyframe[i - 10].shipdata[fid].x + 50f;
                                                                     cameraframe[i].y = keyframe[i - 15].shipdata[fid].y - 50f;
                                                                     cameraframe[i].z = keyframe[i - 20].shipdata[fid].z + 50f;
                                                     }
                                     }
                                     /*
                                         * switch(viewmode) { case 0: //racing towards from a distance - like a pursuit move
                                         *
                                         * if(i==startframe) { cameraframe[i].x = (keyframe[i].shipdata[fid].x)-1500f; cameraframe[i].y = (keyframe[i].shipdata[fid].y)+1500f; cameraframe[i].z =
                                         * (keyframe[i].shipdata[fid].z)-750f; } else { cameraframe[i].x = cameraframe[i-1].x + ((keyframe[i].shipdata[fid].x+350)-cameraframe[i-1].x)*0.15f; cameraframe[i].y = cameraframe[i-1].y
                                         * + ((keyframe[i].shipdata[fid].y+20)-cameraframe[i-1].y)*0.15f; cameraframe[i].z = cameraframe[i-1].z + ((keyframe[i].shipdata[fid].z+135)-cameraframe[i-1].z)*0.15f; } if(i+4<maxframes)
                                         * { cameraframe[i].lookx = (keyframe[i+4].shipdata[fid].x); cameraframe[i].looky = (keyframe[i+4].shipdata[fid].y); cameraframe[i].lookz = (keyframe[i+4].shipdata[fid].z); } else {
                                         * cameraframe[i].lookx = (keyframe[i-4].shipdata[fid].x); cameraframe[i].looky = (keyframe[i-4].shipdata[fid].y); cameraframe[i].lookz = (keyframe[i-4].shipdata[fid].z); }
                                         *
                                         * break; case 1: //racing towards the underneath and as it flys by swinging around to follow
                                         *
                                         * if(i==startframe) { cameraframe[i].x = (keyframe[i].shipdata[fid].x - 800f); cameraframe[i].y = (keyframe[i].shipdata[fid].y - 2000f); cameraframe[i].z = (keyframe[i].shipdata[fid].z -
                                         * 800f)
;
                                         *
                                         * } else { if(i-startframe<200) { cameraframe[i].x = cameraframe[i-1].x + ((keyframe[i].shipdata[fid].x+225f)-cameraframe[i-1].x)*0.1f; cameraframe[i].y = cameraframe[i-1].y +
                                         * ((keyframe[i].shipdata[fid].y-275f)-cameraframe[i-1].y)*0.1f; cameraframe[i].z = cameraframe[i-1].z + ((keyframe[i].shipdata[fid].z+50f)-cameraframe[i-1].z)*0.1f; } else {
                                         * if(i-startframe>400) { cameraframe[i].x = cameraframe[i-1].x + ((keyframe[i].shipdata[fid].x-275f)-cameraframe[i-1].x)*0.1f; cameraframe[i].y = cameraframe[i-1].y +
                                         * ((keyframe[i].shipdata[fid].y+300f)-cameraframe[i-1].y)*0.1f; cameraframe[i].z = cameraframe[i-1].z + ((keyframe[i].shipdata[fid].z-50f)-cameraframe[i-1].z)*0.1f; } else {
                                         * cameraframe[i].x = cameraframe[i-1].x + ((keyframe[i].shipdata[fid].x)-cameraframe[i-1].x)*0.07f; cameraframe[i].y = cameraframe[i-1].y +
                                         * ((keyframe[i].shipdata[fid].y+450f)-cameraframe[i-1].y)*0.07f; cameraframe[i].z = cameraframe[i-1].z + ((keyframe[i].shipdata[fid].z)-cameraframe[i-1].z)*0.07f; } } }
                                         *
                                         * if(i+4<maxframes) { cameraframe[i].lookx = (keyframe[i+4].shipdata[fid].x); cameraframe[i].looky = (keyframe[i+4].shipdata[fid].y); cameraframe[i].lookz =
                                         * (keyframe[i+4].shipdata[fid].z); } else { cameraframe[i].lookx = (keyframe[i-4].shipdata[fid].x); cameraframe[i].looky = (keyframe[i-4].shipdata[fid].y); cameraframe[i].lookz =
                                         * (keyframe[i-4].shipdata[fid].z); }
                                         *
                                         *
                                         * break;
                                         *
                                         * case 2: //sitting behind slightly above or below //orbiting.....
                                         *
                                         * cameraframe[i].lookx = (keyframe[i].shipdata[fid].x-15f); cameraframe[i].looky = (keyframe[i].shipdata[fid].y+15f); cameraframe[i].lookz = (keyframe[i].shipdata[fid].z-20f);
                                         *
                                         * cameraframe[i].x = cameraframe[i].lookx + 70f ; cameraframe[i].y = cameraframe[i].looky - 350f; cameraframe[i].z = cameraframe[i].lookz + 375f;
                                         *
                                         *
                                         * break;
                                         *
                                         * default:
                                         *
                                         * cameraframe[i].lookx = (keyframe[i].shipdata[fid].x-15f); cameraframe[i].looky = (keyframe[i].shipdata[fid].y-25f); cameraframe[i].lookz = (keyframe[i].shipdata[fid].z-10f);
                                         *
                                         * if(i==startframe) { cameraframe[i].x = cameraframe[i].lookx - 270f ; cameraframe[i].y = cameraframe[i].looky + 150f; cameraframe[i].z = cameraframe[i].lookz - 125f; } else {
                                         * cameraframe[i].x = cameraframe[i-1].x + ((keyframe[i].shipdata[fid].x - (keyframe[i-100].shipdata[fid].x)))*0.2f; cameraframe[i].y = cameraframe[i-1].y + ((keyframe[i].shipdata[fid].y -
                                         * (keyframe[i-100].shipdata[fid].y))
)
*0.2f; cameraframe[i].z = cameraframe[i-1].z + ((keyframe[i].shipdata[fid].z - (keyframe[i-100].shipdata[fid].z)))*0.2f; }
                                         *
                                         * cameraframe[i].x+= ((keyframe[i].shipdata[fid].x-300f) - cameraframe[i].x) * 0.175f; cameraframe[i].y+= ((keyframe[i].shipdata[fid].y-300f) - cameraframe[i].y) * 0.175f;
                                         * cameraframe[i].z+= ((keyframe[i].shipdata[fid].z+25f) - cameraframe[i].z) * 0.175f;
                                         *
                                         * break; }
                                         *
/
                     }
                     // if there are none then pick somewhere with action....
                     return fid;
     }

     private int cambigship(int i, int maxframes, int fid, int startframe, int pktm)
     {

                     // the problem with the big ships is that they often don't have a lot of action....
                     // they may explode or be shooting stuff but sometimes they simply get viewed
                     // as a boring sort of individual ship....
                     // we don't want these sort of shots....
                     // not really anyway....
                     // but...we do want the occasional shot with 2 big ships in them (on either same or opposing teams)
                     //

                     int tscore = 0;
                     int score = 0;
                     int shotscore[] = new int[4];

                     if (fid != -1)
                     {
                                     if (keyframe[i].shipdata[fid].hp <= 0)
                                     {
                                                     // reset the fid since the ship has been destroyed - quite
                                                     // common!
                                                     if (keyframe[i - 150].shipdata[fid].hp <= 0) // or we've been
                                                                                                                                                                                                                                         // dead a little
                                                                                                                                                                                                                                         // while...
                                                     {
                                                                     fid = -1;
                                                     }
                                     }
                     }
                     int shotchosen = 0;

                     if (fid == -1)
                     {
                                     // int camshottype = 0;
                                     for (int j = 0; j < 32; j++)
                                     {
                                                     score = 0;
                                                     if (keyframe[i].shiptypes[j] < 3 && keyframe[i].shipdata[j].hp > 0) // big ship
                                                     {
                                                                     // score++;
                                                                     for (int jj = startframe + 30; jj < startframe + 220; jj++)
                                                                     {
                                                                                     if (jj < maxframes)
                                                                                     {
                                                                                                     if (keyframe[jj].shipdata[j].hp != keyframe[jj - 1].shipdata[j].hp)
                                                                                                     {
                                                                                                                     score += 5;
                                                                                                                     // camshottype = 1;
                                                                                                                     shotscore[1]++;
                                                                                                                     if (ships[j].team == pktm)
                                                                                                                     {
                                                                                                                                     score++;
                                                                                                                                     shotscore[1]++;

                                                                                                                     }

                                                                                                     }
                                                                                                     for (int kk = 0; kk < keyframe[jj].shipdata[j].firingframe.length; kk++)
                                                                                                     {
                                                                                                                     if (keyframe[jj].shipdata[j].firingframe[kk] > 0)
                                                                                                                     {
                                                                                                                                     score += 4;
                                                                                                                                     // camshottype = 2;
                                                                                                                                     shotscore[2]++;
                                                                                                                                     if (ships[j].team == pktm)
                                                                                                                                     {
                                                                                                                                                     score++;
                                                                                                                                                     shotscore[2]++;

                                                                                                                                     }
                                                                                                                     }
                                                                                                     }
                                                                                                     if (keyframe[jj].shipdata[j].hp <= 0)
                                                                                                     {
                                                                                                                     score++;
                                                                                                                     // camshottype = 3;
                                                                                                                     shotscore[3]++;
                                                                                                                     if (ships[j].team == pktm)
                                                                                                                     {
                                                                                                                                     score++;
                                                                                                                                     shotscore[3]++;

                                                                                                                     }
                                                                                                     }
                                                                                                     // and even more so---if we split in half!
                                                                                                     if (keyframe[jj].shipdata[j].hp == -10000)
                                                                                                     {
                                                                                                                     score += 10;
                                                                                                                     // camshottype = 3;
                                                                                                                     shotscore[3] += 3;
                                                                                                                     if (ships[j].team == pktm)
                                                                                                                     {
                                                                                                                                     score++;
                                                                                                                                     shotscore[3]++;

                                                                                                                     }
                                                                                                     }

                                                                                     }
                                                                     }
                                                     }
                                                     if (score > tscore)
                                                     {
                                                                     tscore = score;
                                                                     shotchosen = 0;
                                                                     if (shotscore[1] > shotscore[2])
                                                                     {
                                                                                     shotchosen = 1;
                                                                     }
                                                                     else
                                                                     {
                                                                                     shotchosen = 2;
                                                                     }
                                                                     if (shotscore[shotchosen] > shotscore[3])
                                                                     {
                                                                                     // do nothing...
                                                                     }
                                                                     else
                                                                     {
                                                                                     shotchosen = 3;
                                                                     }
                                                                     fid = j;
                                                     }
                                     }
                     }
                     if (fid == -1)
                     {
                                     // Log.i(TAG,"Could not place camera frames:"+i);
                     }
                     else
                     {
                                     switch (shotchosen)
                                     {
                                     case 1: // we are being damaged

                                                     // get an idea for where the damage is coming from.....and
                                                     // include that in the shot.....

                                                     // simply position ourselves at right angles to the ship......

                                                     if (i + 50 < maxframes)
                                                     {
                                                                     cameraframe[i].lookx = keyframe[i + 40].shipdata[fid].x;
                                                                     cameraframe[i].looky = keyframe[i + 35].shipdata[fid].y;
                                                                     cameraframe[i].lookz = keyframe[i + 45].shipdata[fid].z;
                                                     }
                                                     else
                                                     {
                                                                     cameraframe[i].lookx = keyframe[i].shipdata[fid].x;
                                                                     cameraframe[i].looky = keyframe[i].shipdata[fid].y;
                                                                     cameraframe[i].lookz = keyframe[i].shipdata[fid].z;

                                                     }
                                                     int i0 = i - 20;
                                                     int i1 = i + 20;

                                                     if (i1 >= maxframes - 50)
                                                     {
                                                                     i1 = maxframes - 50;
                                                                     i0 = i1 - 50;
                                                     }

                                                     float sx = keyframe[i0].shipdata[fid].x;
                                                     float fx = keyframe[i1].shipdata[fid].x;

                                                     float sy = keyframe[i0].shipdata[fid].y;
                                                     float fy = keyframe[i1].shipdata[fid].y;

                                                     float sz = keyframe[i0].shipdata[fid].z;
                                                     float fz = keyframe[i1].shipdata[fid].z;

                                                     float vx = fx - sx;
                                                     float vy = fy - sy;
                                                     float vz = fz - sz;

                                                     float vlen = (float) Math.sqrt(vx * vx + vy * vy + vz + vz);
                                                     if (vlen > 0)
                                                     {
                                                                     vx /= vlen;
                                                                     vy /= vlen;
                                                                     vz /= vlen;
                                                     }

                                                     // find a vector perpendicular to this one....
                                                     // good old dot product again.....

                                                     // dot = a.b = ax * bx + ay * by + az * bz;
                                                     // at right angles when = 0....
                                                     // let bz = 0;
                                                     // bx =-ay and by = ax.....

                                                     float bx = vy;
                                                     float by = -vx;
                                                     float bz = 0;

                                                     float bfactor = 700f + (i - (startframe + 200)) * 0.85f;

                                                     if (i == startframe)
                                                     {
                                                                     cameraframe[i].x = keyframe[i].shipdata[fid].x + bfactor * bx;
                                                                     cameraframe[i].y = keyframe[i].shipdata[fid].y + bfactor * by;
                                                                     cameraframe[i].z = keyframe[i].shipdata[fid].z + bfactor * bz;
                                                     }
                                                     else
                                                     {
                                                                     cameraframe[i].x = cameraframe[i - 1].x + ((keyframe[i].shipdata[fid].x + bfactor * bx) - cameraframe[i - 1].x) * 0.005f;
                                                                     cameraframe[i].y = cameraframe[i - 1].y + ((keyframe[i].shipdata[fid].y + bfactor * by) - cameraframe[i - 1].y) * 0.005f;
                                                                     cameraframe[i].z = cameraframe[i - 1].z + ((keyframe[i].shipdata[fid].z + bfactor * bz) - cameraframe[i - 1].z) * 0.005f;
                                                     }
                                                     break;

                                     case 2: // we are shooting

                                                     // look at what are shooting at.....

                                                     // typically shooting straight ahead...so look ahead.....

                                                     sx = keyframe[i].shipdata[fid].x;
                                                     sy = keyframe[i].shipdata[fid].y;
                                                     sz = keyframe[i].shipdata[fid].z;

                                                     fx = sx;
                                                     fy = sy;
                                                     fz = sz + 0.1f;

                                                     if (i + 10 < maxframes)
                                                     {
                                                                     fx = keyframe[i + 8].shipdata[fid].x;
                                                                     fy = keyframe[i + 8].shipdata[fid].y;
                                                                     fz = keyframe[i + 8].shipdata[fid].z;
                                                     }

                                                     vx = fx - sx;
                                                     vy = fy - sy;
                                                     vz = fz - sz;

                                                     vlen = (float) Math.sqrt(vx * vx + vy * vy + vz * vz);
                                                     if (vlen > 0)
                                                     {
                                                                     vx /= vlen;
                                                                     vy /= vlen;
                                                                     vz /= vlen;
                                                     }
                                                     else
                                                     {
                                                                     vx = 0;
                                                                     vy = 0;
                                                                     vz = 1;
                                                     }
                                                     if (i == startframe)
                                                     {
                                                                     cameraframe[i].lookx = sx + vx * 700f;
                                                                     cameraframe[i].looky = sy + vy * 700f;
                                                                     cameraframe[i].lookz = sz + vz * 700f;

                                                                     cameraframe[i].x = -15f + sx - vx * 300f;
                                                                     cameraframe[i].y = -10f + sy - vy * 300f;
                                                                     cameraframe[i].z = -50f + sz - vz * 300f;

                                                     }
                                                     else
                                                     {
                                                                     cameraframe[i].lookx = cameraframe[i - 1].lookx + (sx + vx * 700f - cameraframe[i - 1].lookx) * 0.1f;
                                                                     cameraframe[i].looky = cameraframe[i - 1].looky + (sy + vy * 700f - cameraframe[i - 1].looky) * 0.1f;
                                                                     cameraframe[i].lookz = cameraframe[i - 1].lookz + (sz + vz * 700f - cameraframe[i - 1].lookz) * 0.1f;

                                                                     cameraframe[i].x = cameraframe[i - 1].x + (-15f + sx - vx * 300f - cameraframe[i - 1].x) * 0.07f;
                                                                     cameraframe[i].y = cameraframe[i - 1].y + (-10f + sy - vy * 300f - cameraframe[i - 1].y) * 0.07f;
                                                                     cameraframe[i].z = cameraframe[i - 1].z + (-50f + sz - vz * 300f - cameraframe[i - 1].z) * 0.07f;
                                                     }

                                                     break;

                                     case 3: // we are blowing up

                                                     // look at us....

                                                     if (i + 20 < maxframes)
                                                     {
                                                                     cameraframe[i].lookx = keyframe[i + 10].shipdata[fid].x;
                                                                     cameraframe[i].looky = keyframe[i + 15].shipdata[fid].y;
                                                                     cameraframe[i].lookz = keyframe[i + 15].shipdata[fid].z;
                                                     }
                                                     else
                                                     {
                                                                     cameraframe[i].lookx = keyframe[i].shipdata[fid].x;
                                                                     cameraframe[i].looky = keyframe[i].shipdata[fid].y;
                                                                     cameraframe[i].lookz = keyframe[i].shipdata[fid].z;

                                                     }
                                                     cameraframe[i].x = keyframe[i].shipdata[fid].x - 300f;
                                                     cameraframe[i].y = keyframe[i].shipdata[fid].y - (650f + (float) (i - startframe) * 0.25f);
                                                     cameraframe[i].z = keyframe[i].shipdata[fid].z - 300f;

                                                     break;

                                     default: // nothing special....

                                                     if (i + 20 < maxframes)
                                                     {
                                                                     cameraframe[i].lookx = keyframe[i + 10].shipdata[fid].x;
                                                                     cameraframe[i].looky = keyframe[i + 15].shipdata[fid].y;
                                                                     cameraframe[i].lookz = keyframe[i + 12].shipdata[fid].z;
                                                     }
                                                     else
                                                     {
                                                                     cameraframe[i].lookx = keyframe[i].shipdata[fid].x;
                                                                     cameraframe[i].looky = keyframe[i].shipdata[fid].y;
                                                                     cameraframe[i].lookz = keyframe[i].shipdata[fid].z;

                                                     }

                                                     cameraframe[i].x = keyframe[i].shipdata[fid].x - (200f + (float) (i - startframe) * 0.05f);
                                                     cameraframe[i].y = keyframe[i].shipdata[fid].y - (250f + (float) (i - startframe) * 0.05f);
                                                     cameraframe[i].z = keyframe[i].shipdata[fid].z - (150f + (float) (i - startframe) * 0.05f);

                                                     break;
                                     }
                     }
                     return fid;
     }

     private int camaction(int i, int maxframes, int startframe, int fid, int pktm)
     {
                     if (i == startframe)
                     {
                                     // Log.i(TAG,"Cam Action:"+i);
                     }

                     // examine this frame only
                     int jframe = 0;
                     int gid = -1;
                     if (fid != -1)
                     {
                                     if (keyframe[i].shipdata[fid].hp <= 0)
                                     {
                                                     // reset the fid since the ship has been destroyed - quite
                                                     // common!
                                                     if (keyframe[i - 150].shipdata[fid].hp <= 0) // or we've been
                                                                                                                                                                                                                                         // dead a little
                                                                                                                                                                                                                                         // while...
                                                     {
                                                                     fid = -1;
                                                     }
                                     }
                     }
                     if (fid == -1)
                     {
                                     for (jframe = i; jframe < i + 1; jframe++)
                                     {
                                                     if (jframe < maxframes)
                                                     {
                                                                     for (int j = 0; j < 32; j++)
                                                                     {

                                                                                     if (keyframe[jframe].shipdata[j].hp > 0)
                                                                                     {
                                                                                                     fid = j;
                                                                                                     float x1 = keyframe[jframe].shipdata[j].x;
                                                                                                     float y1 = keyframe[jframe].shipdata[j].y;
                                                                                                     float z1 = keyframe[jframe].shipdata[j].z;

                                                                                                     float x2 = 0;
                                                                                                     float y2 = 0;
                                                                                                     float z2 = 0;

                                                                                                     for (int jj = 0; jj < 32; jj++)
                                                                                                     {
                                                                                                                     if (keyframe[jframe].shipdata[jj].hp > 0 && ships[j].team != ships[jj].team)
                                                                                                                     {
                                                                                                                                     x2 = keyframe[jframe].shipdata[jj].x;
                                                                                                                                     y2 = keyframe[jframe].shipdata[jj].y;
                                                                                                                                     z2 = keyframe[jframe].shipdata[jj].z;

                                                                                                                                     if ((Math.abs(x1 - x2) < 600) && Math.abs(y1 - y2) < 600 && Math.abs(z1 - z2) < 600)
                                                                                                                                     {
                                                                                                                                                     // check if either of these ships takes
                                                                                                                                                     // damage in the next few frames......
                                                                                                                                                     for (int k = jframe + 1; k < jframe + 275; k++)
                                                                                                                                                     {
                                                                                                                                                                     if (k < maxframes)
                                                                                                                                                                     {
                                                                                                                                                                                     if (keyframe[jframe].shipdata[j].hp > keyframe[k].shipdata[j].hp || keyframe[jframe].shipdata[jj].hp > keyframe[k].shipdata[jj].hp)
                                                                                                                                                                                     {
                                                                                                                                                                                                     // pick this ship......
                                                                                                                                                                                                     gid = jj;
                                                                                                                                                                                     }
                                                                                                                                                                     }
                                                                                                                                                     }
                                                                                                                                     }
                                                                                                                     }
                                                                                                     }
                                                                                     }
                                                                     }
                                                     }
                                     }
                     }
                     else
                     {
                                     for (jframe = i; jframe < i + 1; jframe++)
                                     {
                                                     if (jframe < maxframes)
                                                     {
                                                                     int j = fid;
                                                                     {

                                                                                     if (keyframe[jframe].shipdata[j].hp > 0)
                                                                                     {
                                                                                                     fid = j;
                                                                                                     float x1 = keyframe[jframe].shipdata[j].x;
                                                                                                     float y1 = keyframe[jframe].shipdata[j].y;
                                                                                                     float z1 = keyframe[jframe].shipdata[j].z;

                                                                                                     float x2 = 0;
                                                                                                     float y2 = 0;
                                                                                                     float z2 = 0;

                                                                                                     for (int jj = 0; jj < 32; jj++)
                                                                                                     {
                                                                                                                     if (keyframe[jframe].shipdata[jj].hp > 0 && ships[j].team != ships[jj].team)
                                                                                                                     {
                                                                                                                                     x2 = keyframe[jframe].shipdata[jj].x;
                                                                                                                                     y2 = keyframe[jframe].shipdata[jj].y;
                                                                                                                                     z2 = keyframe[jframe].shipdata[jj].z;

                                                                                                                                     if ((Math.abs(x1 - x2) < 600) && Math.abs(y1 - y2) < 600 && Math.abs(z1 - z2) < 600)
                                                                                                                                     {
                                                                                                                                                     // check if either of these ships takes
                                                                                                                                                     // damage in the next few frames......
                                                                                                                                                     for (int k = jframe + 1; k < jframe + 275; k++)
                                                                                                                                                     {
                                                                                                                                                                     if (k < maxframes)
                                                                                                                                                                     {
                                                                                                                                                                                     if (keyframe[jframe].shipdata[j].hp > keyframe[k].shipdata[j].hp || keyframe[jframe].shipdata[jj].hp > keyframe[k].shipdata[jj].hp)
                                                                                                                                                                                     {
                                                                                                                                                                                                     // pick this ship......
                                                                                                                                                                                                     gid = jj;
                                                                                                                                                                                     }
                                                                                                                                                                     }
                                                                                                                                                     }
                                                                                                                                     }
                                                                                                                     }
                                                                                                     }
                                                                                     }
                                                                     }
                                                     }
                                     }
                     }
                     if (gid != -1)
                     {
                                     // line the camera up at these two......

                                     // position at or near fid and look at gid.....

                                     if (i == startframe)
                                     {
                                                     cameraframe[i].x = keyframe[i].shipdata[fid].x + 40f + (keyframe[i].shipdata[fid].x - keyframe[i].shipdata[gid].x) * 0.5f;
                                                     cameraframe[i].y = keyframe[i].shipdata[fid].y + 20f + (keyframe[i].shipdata[fid].y - keyframe[i].shipdata[gid].y) * 0.5f;
                                                     cameraframe[i].z = keyframe[i].shipdata[fid].z + 60f + (keyframe[i].shipdata[fid].z - keyframe[i].shipdata[gid].z) * 0.5f;

                                                     // always look ahead of the target slightly....if it is a big ship the a look a few more frames in front of it...
                                                     if (ships[gid].shiptype < 3)
                                                     {
                                                                     if (i < maxframes - 100)
                                                                     {
                                                                                     cameraframe[i].lookx = keyframe[i + 30].shipdata[gid].x;
                                                                                     cameraframe[i].looky = keyframe[i + 35].shipdata[gid].y;
                                                                                     cameraframe[i].lookz = keyframe[i + 40].shipdata[gid].z;

                                                                     }
                                                                     else
                                                                     {
                                                                                     cameraframe[i].lookx = keyframe[i].shipdata[gid].x;
                                                                                     cameraframe[i].looky = keyframe[i].shipdata[gid].y;
                                                                                     cameraframe[i].lookz = keyframe[i].shipdata[gid].z;

                                                                     }
                                                     }
                                                     else
                                                     {
                                                                     if (i < maxframes - 10)
                                                                     {
                                                                                     cameraframe[i].lookx = keyframe[i + 6].shipdata[gid].x;
                                                                                     cameraframe[i].looky = keyframe[i + 7].shipdata[gid].y;
                                                                                     cameraframe[i].lookz = keyframe[i + 8].shipdata[gid].z;

                                                                     }
                                                                     else
                                                                     {
                                                                                     cameraframe[i].lookx = keyframe[i].shipdata[gid].x;
                                                                                     cameraframe[i].looky = keyframe[i].shipdata[gid].y;
                                                                                     cameraframe[i].lookz = keyframe[i].shipdata[gid].z;
                                                                     }
                                                     }

                                     }
                                     else
                                     {
                                                     cameraframe[i].x = keyframe[i].shipdata[fid].x + 40f + (keyframe[i].shipdata[fid].x - keyframe[i].shipdata[gid].x) * 0.5f;
                                                     cameraframe[i].y = keyframe[i].shipdata[fid].y + 20f + (keyframe[i].shipdata[fid].y - keyframe[i].shipdata[gid].y) * 0.5f;
                                                     cameraframe[i].z = keyframe[i].shipdata[fid].z + 60f + (keyframe[i].shipdata[fid].z - keyframe[i].shipdata[gid].z) * 0.5f;

                                                     if (ships[gid].shiptype < 3)
                                                     {
                                                                     if (i < maxframes - 100)
                                                                     {
                                                                                     cameraframe[i].lookx = keyframe[i + 35].shipdata[gid].x;
                                                                                     cameraframe[i].looky = keyframe[i + 30].shipdata[gid].y;
                                                                                     cameraframe[i].lookz = keyframe[i + 45].shipdata[gid].z;

                                                                     }
                                                                     else
                                                                     {
                                                                                     cameraframe[i].lookx = keyframe[i].shipdata[gid].x;
                                                                                     cameraframe[i].looky = keyframe[i].shipdata[gid].y;
                                                                                     cameraframe[i].lookz = keyframe[i].shipdata[gid].z;

                                                                     }
                                                     }
                                                     else
                                                     {
                                                                     if (i < maxframes - 10)
                                                                     {
                                                                                     cameraframe[i].lookx = keyframe[i + 7].shipdata[gid].x;
                                                                                     cameraframe[i].looky = keyframe[i + 6].shipdata[gid].y;
                                                                                     cameraframe[i].lookz = keyframe[i + 9].shipdata[gid].z;

                                                                     }
                                                                     else
                                                                     {
                                                                                     cameraframe[i].lookx = keyframe[i].shipdata[gid].x;
                                                                                     cameraframe[i].looky = keyframe[i].shipdata[gid].y;
                                                                                     cameraframe[i].lookz = keyframe[i].shipdata[gid].z;
                                                                     }
                                                     }

                                     }

                     }
                     else
                     {
                                     if (fid != -1)
                                     {
                                                     // no other ship selected....so just do the standard thing.....
                                                     // Log.i(TAG,"CAM ACTION - NO GID");
                                                     float dfactor = 2.0f;
                                                     if (ships[fid].sizetype > 4)
                                                     {
                                                                     dfactor = 4.0f;
                                                     }

                                                     if (i == startframe)
                                                     {
                                                                     cameraframe[i].x = (keyframe[i].shipdata[fid].x - dfactor * 100f);
                                                                     cameraframe[i].y = (keyframe[i].shipdata[fid].y + dfactor * 50f);
                                                                     cameraframe[i].z = (keyframe[i].shipdata[fid].z + dfactor * 125f);
                                                     }
                                                     else
                                                     {
                                                                     if (i - startframe < 200)
                                                                     {
                                                                                     cameraframe[i].x = cameraframe[i - 1].x + ((keyframe[i].shipdata[fid].x + 175f * dfactor) - cameraframe[i - 1].x) * 0.1f;
                                                                                     cameraframe[i].y = cameraframe[i - 1].y + ((keyframe[i].shipdata[fid].y - 125f * dfactor) - cameraframe[i - 1].y) * 0.1f;
                                                                                     cameraframe[i].z = cameraframe[i - 1].z + ((keyframe[i].shipdata[fid].z + 200f * dfactor) - cameraframe[i - 1].z) * 0.1f;
                                                                     }
                                                                     else
                                                                     {
                                                                                     if (i - startframe > 475)
                                                                                     {
                                                                                                     cameraframe[i].x = cameraframe[i - 1].x + ((keyframe[i].shipdata[fid].x - 75f * dfactor) - cameraframe[i - 1].x) * 0.1f;
                                                                                                     cameraframe[i].y = cameraframe[i - 1].y + ((keyframe[i].shipdata[fid].y + 100f * dfactor) - cameraframe[i - 1].y) * 0.1f;
                                                                                                     cameraframe[i].z = cameraframe[i - 1].z + ((keyframe[i].shipdata[fid].z - 120f * dfactor) - cameraframe[i - 1].z) * 0.1f;
                                                                                     }
                                                                                     else
                                                                                     {
                                                                                                     cameraframe[i].x = cameraframe[i - 1].x + ((keyframe[i].shipdata[fid].x - 80 * dfactor) - cameraframe[i - 1].x) * 0.1f;
                                                                                                     cameraframe[i].y = cameraframe[i - 1].y + ((keyframe[i].shipdata[fid].y - 165 * dfactor) - cameraframe[i - 1].y) * 0.1f;
                                                                                                     cameraframe[i].z = cameraframe[i - 1].z + ((keyframe[i].shipdata[fid].z + 155 * dfactor) - cameraframe[i - 1].z) * 0.1f;
                                                                                     }
                                                                     }
                                                     }
                                                     if (i < maxframes - 10)
                                                     {
                                                                     cameraframe[i].lookx = (keyframe[i + 7].shipdata[fid].x);
                                                                     cameraframe[i].looky = (keyframe[i + 5].shipdata[fid].y);
                                                                     cameraframe[i].lookz = (keyframe[i + 6].shipdata[fid].z);
                                                     }
                                                     else
                                                     {
                                                                     cameraframe[i].lookx = (keyframe[i].shipdata[fid].x);
                                                                     cameraframe[i].looky = (keyframe[i].shipdata[fid].y);
                                                                     cameraframe[i].lookz = (keyframe[i].shipdata[fid].z);

                                                     }
                                     }
                                     else
                                     {
                                                     // Log.i(TAG,"Huh no fid?");
                                     }
                     }
                     // Log.i(TAG,"fid:"+fid+" gid:"+gid+" frame:"+i+" startframe:"+startframe);

                     return fid;
     }

     private void camwideshot(int i, int maxframes, int team, int startframe)
     {
                     // opening scenes....
                     float sumx = 0;
                     float sumy = 0;
                     float sumz = 0;
                     float count = 0;

                     float minx = 99999;
                     float maxx = -99999;
                     float miny = 99999;
                     float maxy = -99999;
                     float minz = 99999;
                     float maxz = -99999;
                     int jframe = 0;

                     for (int j = 0; j < 32; j++)
                     {
                                     if (keyframe[i].shipdata[j].hp > 0)
                                     {
                                                     // look at where they will be in 150 frames....
                                                     jframe = i + 150;
                                                     if (jframe >= maxframes)
                                                     {
                                                                     jframe = maxframes - 1;
                                                     }
                                                     sumx += keyframe[jframe].shipdata[j].x;
                                                     sumy += keyframe[jframe].shipdata[j].y;
                                                     sumz += keyframe[jframe].shipdata[j].z;
                                                     count++;
                                                     if (ships[j].sizetype > 4 || ships[j].team == dteam)
                                                     {
                                                                     sumx += 2 * keyframe[jframe].shipdata[j].x;
                                                                     sumy += 2 * keyframe[jframe].shipdata[j].y;
                                                                     sumz += 2 * keyframe[jframe].shipdata[j].z;
                                                                     count += 2;

                                                     }
                                                     if (keyframe[jframe].shipdata[j].x < minx)
                                                     {
                                                                     minx = keyframe[jframe].shipdata[j].x;
                                                     }
                                                     if (keyframe[jframe].shipdata[j].x > maxx)
                                                     {
                                                                     maxx = keyframe[jframe].shipdata[j].x;
                                                     }

                                                     if (keyframe[jframe].shipdata[j].y < miny)
                                                     {
                                                                     miny = keyframe[jframe].shipdata[j].y;
                                                     }
                                                     if (keyframe[jframe].shipdata[j].y > maxy)
                                                     {
                                                                     maxy = keyframe[jframe].shipdata[j].y;
                                                     }
                                                     if (keyframe[jframe].shipdata[j].z < minz)
                                                     {
                                                                     minz = keyframe[jframe].shipdata[j].z;
                                                     }
                                                     if (keyframe[jframe].shipdata[j].z > maxz)
                                                     {
                                                                     maxz = keyframe[jframe].shipdata[j].z;
                                                     }
                                     }
                     }
                     if (count > 0)
                     {
                                     sumx /= count;
                                     sumy /= count;
                                     sumz /= count;
                     }
                     // look at where the ships will be in 100 frames....on average...
                     cameraframe[i].lookx = sumx;
                     cameraframe[i].looky = sumy;
                     cameraframe[i].lookz = sumz;

                     sumx = 0;
                     sumy = 0;
                     sumz = 0;
                     count = 0;

                     minx = 99999;
                     maxx = -99999;
                     miny = 99999;
                     maxy = -99999;
                     minz = 99999;
                     maxz = -99999;

                     for (int j = 0; j < 32; j++)
                     {
                                     if (keyframe[i].shipdata[j].hp > 0 && ships[j].team == ateam)
                                     {
                                                     jframe = i - 150;
                                                     if (jframe < 11)
                                                     {
                                                                     jframe = 11;
                                                     }
                                                     if (jframe >= maxframes)
                                                     {
                                                                     jframe = maxframes - 1;
                                                     }

                                                     sumx += keyframe[jframe].shipdata[j].x;
                                                     sumy += keyframe[jframe].shipdata[j].y;
                                                     sumz += keyframe[jframe].shipdata[j].z;
                                                     count++;

                                                     if (ships[j].sizetype > 4)
                                                     {
                                                                     sumx += 2 * keyframe[jframe].shipdata[j].x;
                                                                     sumy += 2 * keyframe[jframe].shipdata[j].y;
                                                                     sumz += 2 * keyframe[jframe].shipdata[j].z;
                                                                     count += 2;
                                                     }

                                                     if (keyframe[jframe].shipdata[j].x < minx)
                                                     {
                                                                     minx = keyframe[jframe].shipdata[j].x;
                                                     }
                                                     if (keyframe[jframe].shipdata[j].x > maxx)
                                                     {
                                                                     maxx = keyframe[jframe].shipdata[j].x;
                                                     }

                                                     if (keyframe[jframe].shipdata[j].y < miny)
                                                     {
                                                                     miny = keyframe[jframe].shipdata[j].y;
                                                     }
                                                     if (keyframe[jframe].shipdata[j].y > maxy)
                                                     {
                                                                     maxy = keyframe[jframe].shipdata[j].y;
                                                     }

                                                     if (keyframe[jframe].shipdata[j].z < minz)
                                                     {
                                                                     minz = keyframe[jframe].shipdata[j].z;
                                                     }
                                                     if (keyframe[jframe].shipdata[j].z > maxz)
                                                     {
                                                                     maxz = keyframe[jframe].shipdata[j].z;
                                                     }
                                     }
                     }
                     if (count > 0)
                     {
                                     sumx /= count;
                                     sumy /= count;
                                     sumz /= count;
                     }
                     // look at where the ships will be in 100 frames....on average...
                     if (i > startframe)
                     {
                                     cameraframe[i].x = cameraframe[i - 1].x + ((sumx + 275f) - cameraframe[i - 1].x) * 0.01f;
                                     cameraframe[i].y = cameraframe[i - 1].y + ((sumy - 500f) - cameraframe[i - 1].y) * 0.01f;
                                     float dd = (sumz - cameraframe[i].lookz) * 0.0125f;
                                     cameraframe[i].z = cameraframe[i - 1].z + ((sumz + dd) - cameraframe[i - 1].z) * 0.01f;
                     }
                     else
                     {

                                     cameraframe[i].x = sumx + 300f;
                                     cameraframe[i].y = sumy - 850f;
                                     float dd = (sumz - cameraframe[i].lookz) * 0.25f;
                                     cameraframe[i].z = sumz + dd;
                     }
     }

     private boolean iwillcollidewithyou(int me, int you)
     {
                     // every time I read this function name I hear the song by Gloria Gaynor - "I will survive...." and think of Priscilla Queen of the Desert
                     // look at my vx,vy,vz

                     // if our paths intersect between now and 'n' frames then that is bad!
                     float VDx = ships[you].vx - ships[me].vx;
                     float VDy = ships[you].vy - ships[me].vy;
                     float VDz = ships[you].vz - ships[me].vz;

                     float Dx = ships[you].x - ships[me].x;
                     float Dy = ships[you].y - ships[me].y;
                     float Dz = ships[you].z - ships[me].z;

                     float DxDx = Dx * Dx;
                     float DyDy = Dy * Dy;
                     float DzDz = Dz * Dz;

                     float DxVDx = Dx * VDx;
                     float DyVDy = Dy * VDy;
                     float DzVDz = Dz * VDz;

                     float VDxVDx = VDx * VDx;
                     float VDyVDy = VDy * VDy;
                     float VDzVDz = VDz * VDz;

                     float LL = ships[me].maxvel * ships[me].maxvel;

                     if (LL == VDxVDx + VDyVDy + VDzVDz)
                     {
                                     return false;
                     }
                     float BB = (DxVDx + DyVDy + DzVDz) * (DxVDx + DyVDy + DzVDz);
                     float AC = (LL - VDxVDx - VDyVDy - VDzVDz) * (DxDx + DyDy + DzDz);

                     if (BB + AC < 0)
                     {
                                     return false;
                     }
                     float sqrBBAC = (float) Math.sqrt(BB + AC);
                     float T1 = ((DxVDx + DyVDy + DzVDz) + sqrBBAC) / (LL - (VDxVDx + VDyVDy + VDzVDz));
                     float T2 = ((DxVDx + DyVDy + DzVDz) - sqrBBAC) / (LL - (VDxVDx + VDyVDy + VDzVDz));

                     float T = 0;
                     if (T1 < 0)
                     {
                                     if (T2 > 0)
                                     {
                                                     T = T2;
                                     }
                     }
                     if (T2 < 0)
                     {
                                     if (T1 > 0)
                                     {
                                                     T = T1;
                                     }
                     }

                     if (T1 < T2)
                     {
                                     T = T1;
                     }
                     if (T2 > 0)
                     {
                                     T = T2;
                     }

                     float minF = 90; // 1 and a half seconds to pull up! roughly....
                     if (ships[me].shiptype > 2)
                     {
                                     // we are small and agile....make minF smaller....
                                     minF = 15;
                                     if (ships[me].shiptype == 4)
                                     {
                                                     minF = 10; // we are really, really agile!
                                     }
                     }
                     if (ships[me].shiptype < 3)
                     {
                                     // we are the big ugly ship....no room to move here!
                                     // minF = 400;
                     }
                     if (ships[you].shiptype < 3)
                     {
                                     // minF = minF * 1.25f;
                     }

                     if (T > 0 && T < minF)
                     {
                                     return true;
                     }
                     // otherwise won't hit...
                     return false;
     }

     private void sendprogressupdate(final float p)
     {
                     try
                     {
                                     Intent broadcastintent = new Intent("com.lloydm.geosword.activities.LaunchAttackActivity.simprogress");
                                     broadcastintent.putExtra("com.lloydm.geosword.activities.LaunchAttackActivity.simprogvalue", p);
                                     srvc.sendBroadcast(broadcastintent);
                     }
                     catch (Exception e)
                     {
                                     Log.e(TAG, "broadcast failed from simulator");
                     }
     }

     private void setlocalai(SharedPreferences sp, boolean bigshakeup)
     {
                     SharedPreferences.Editor edit = sp.edit();
                     Random rand = new Random();
                     for (int i = 16; i < 32; i++)
                     {
                                     for (int j = 0; j < 16; j++)
                                     {

                                                     if (bigshakeup)
                                                     {
                                                                     if (rand.nextInt(100) < 40)
                                                                     {
                                                                                     edit.putString("com.lloydm.geoswordlite.activities.ModifyAIActivity.ai_" + i + "_" + j, Integer.toString(rand.nextInt(10)));
                                                                     }
                                                     }
                                                     else
                                                     {
                                                                     if (rand.nextInt(100) < 10)
                                                                     {
                                                                                     edit.putString("com.lloydm.geoswordlite.activities.ModifyAIActivity.ai_" + i + "_" + j, Integer.toString(rand.nextInt(10)));
                                                                     }
                                                     }
                                     }
                     }

                     edit.commit();
     }

     private void getlocaleai(SharedPreferences sp)
     {
                     // random!for now.....
                     Random rand = new Random();
                     for (int i = 16; i < 32; i++)
                     {
                                     for (int j = 0; j < 16; j++)
                                     {
                                                     // ships[i].aislidervalues[j] = rand.nextInt(10);
                                                     ships[i].aislidervalues[j] = Integer.decode(sp.getString("com.lloydm.geosword.activities.ModifyAIActivity.ai_" + i + "_" + j, Integer.toString(rand.nextInt(10))));
                                     }
                     }
     }

     private void getlocalai()
     {
                     // ("com.lloydm.geosword.activities.ModifyAIActivity.ai_"+shiptypeid+"_"+seekBar.getId(), Integer.toString(seekBar.getProgress()));
                     SharedPreferences sp = this.srvc.getSharedPreferences("com.lloydm.geosword.common.stdgameprefs", Activity.MODE_PRIVATE);

                     getlocaleai(sp);

                     for (int i = 0; i < 16; i++)
                     {
                                     if (i == 0)
                                     {
                                                     ships[0].aislidervalues[0] = Integer.decode(sp.getString("com.lloydm.geosword.activities.ModifyAIActivity.ai_1_0", "5"));
                                                     ships[0].aislidervalues[1] = Integer.decode(sp.getString("com.lloydm.geosword.activities.ModifyAIActivity.ai_1_1", "5"));
                                                     ships[0].aislidervalues[2] = Integer.decode(sp.getString("com.lloydm.geosword.activities.ModifyAIActivity.ai_1_2", "5"));
                                                     ships[0].aislidervalues[3] = Integer.decode(sp.getString("com.lloydm.geosword.activities.ModifyAIActivity.ai_1_3", "5"));
                                                     ships[0].aislidervalues[4] = Integer.decode(sp.getString("com.lloydm.geosword.activities.ModifyAIActivity.ai_1_4", "5"));
                                                     ships[0].aislidervalues[5] = Integer.decode(sp.getString("com.lloydm.geosword.activities.ModifyAIActivity.ai_1_5", "5"));
                                                     ships[0].aislidervalues[6] = Integer.decode(sp.getString("com.lloydm.geosword.activities.ModifyAIActivity.ai_1_6", "5"));
                                                     ships[0].aislidervalues[7] = Integer.decode(sp.getString("com.lloydm.geosword.activities.ModifyAIActivity.ai_1_7", "5"));
                                                     ships[0].aislidervalues[8] = Integer.decode(sp.getString("com.lloydm.geosword.activities.ModifyAIActivity.ai_1_8", "5"));
                                                     ships[0].aislidervalues[9] = Integer.decode(sp.getString("com.lloydm.geosword.activities.ModifyAIActivity.ai_1_9", "5"));
                                                     ships[0].aislidervalues[10] = Integer.decode(sp.getString("com.lloydm.geosword.activities.ModifyAIActivity.ai_1_10", "5"));
                                                     ships[0].aislidervalues[11] = Integer.decode(sp.getString("com.lloydm.geosword.activities.ModifyAIActivity.ai_1_11", "5"));
                                                     ships[0].aislidervalues[12] = Integer.decode(sp.getString("com.lloydm.geosword.activities.ModifyAIActivity.ai_1_12", "5"));
                                                     ships[0].aislidervalues[13] = Integer.decode(sp.getString("com.lloydm.geosword.activities.ModifyAIActivity.ai_1_13", "5"));
                                                     ships[0].aislidervalues[14] = Integer.decode(sp.getString("com.lloydm.geosword.activities.ModifyAIActivity.ai_1_14", "5"));
                                                     ships[0].aislidervalues[15] = Integer.decode(sp.getString("com.lloydm.geosword.activities.ModifyAIActivity.ai_1_15", "5"));
                                     }
                                     if (i > 0 && i < 4)
                                     {
                                                     ships[i].aislidervalues[0] = Integer.decode(sp.getString("com.lloydm.geosword.activities.ModifyAIActivity.ai_2_0", "5"));
                                                     ships[i].aislidervalues[1] = Integer.decode(sp.getString("com.lloydm.geosword.activities.ModifyAIActivity.ai_2_1", "5"));
                                                     ships[i].aislidervalues[2] = Integer.decode(sp.getString("com.lloydm.geosword.activities.ModifyAIActivity.ai_2_2", "5"));
                                                     ships[i].aislidervalues[3] = Integer.decode(sp.getString("com.lloydm.geosword.activities.ModifyAIActivity.ai_2_3", "5"));
                                                     ships[i].aislidervalues[4] = Integer.decode(sp.getString("com.lloydm.geosword.activities.ModifyAIActivity.ai_2_4", "5"));
                                                     ships[i].aislidervalues[5] = Integer.decode(sp.getString("com.lloydm.geosword.activities.ModifyAIActivity.ai_2_5", "5"));
                                                     ships[i].aislidervalues[6] = Integer.decode(sp.getString("com.lloydm.geosword.activities.ModifyAIActivity.ai_2_6", "5"));
                                                     ships[i].aislidervalues[7] = Integer.decode(sp.getString("com.lloydm.geosword.activities.ModifyAIActivity.ai_2_7", "5"));
                                                     ships[i].aislidervalues[8] = Integer.decode(sp.getString("com.lloydm.geosword.activities.ModifyAIActivity.ai_2_8", "5"));
                                                     ships[i].aislidervalues[9] = Integer.decode(sp.getString("com.lloydm.geosword.activities.ModifyAIActivity.ai_2_9", "5"));
                                                     ships[i].aislidervalues[10] = Integer.decode(sp.getString("com.lloydm.geosword.activities.ModifyAIActivity.ai_2_10", "5"));
                                                     ships[i].aislidervalues[11] = Integer.decode(sp.getString("com.lloydm.geosword.activities.ModifyAIActivity.ai_2_11", "5"));
                                                     ships[i].aislidervalues[12] = Integer.decode(sp.getString("com.lloydm.geosword.activities.ModifyAIActivity.ai_2_12", "5"));
                                                     ships[i].aislidervalues[13] = Integer.decode(sp.getString("com.lloydm.geosword.activities.ModifyAIActivity.ai_2_13", "5"));
                                                     ships[i].aislidervalues[14] = Integer.decode(sp.getString("com.lloydm.geosword.activities.ModifyAIActivity.ai_2_14", "5"));
                                                     ships[i].aislidervalues[15] = Integer.decode(sp.getString("com.lloydm.geosword.activities.ModifyAIActivity.ai_2_15", "5"));
                                     }
                                     if (i > 3 && i < 8)
                                     {
                                                     ships[i].aislidervalues[0] = Integer.decode(sp.getString("com.lloydm.geosword.activities.ModifyAIActivity.ai_3_0", "5"));
                                                     ships[i].aislidervalues[1] = Integer.decode(sp.getString("com.lloydm.geosword.activities.ModifyAIActivity.ai_3_1", "5"));
                                                     ships[i].aislidervalues[2] = Integer.decode(sp.getString("com.lloydm.geosword.activities.ModifyAIActivity.ai_3_2", "5"));
                                                     ships[i].aislidervalues[3] = Integer.decode(sp.getString("com.lloydm.geosword.activities.ModifyAIActivity.ai_3_3", "5"));
                                                     ships[i].aislidervalues[4] = Integer.decode(sp.getString("com.lloydm.geosword.activities.ModifyAIActivity.ai_3_4", "5"));
                                                     ships[i].aislidervalues[5] = Integer.decode(sp.getString("com.lloydm.geosword.activities.ModifyAIActivity.ai_3_5", "5"));
                                                     ships[i].aislidervalues[6] = Integer.decode(sp.getString("com.lloydm.geosword.activities.ModifyAIActivity.ai_3_6", "5"));
                                                     ships[i].aislidervalues[7] = Integer.decode(sp.getString("com.lloydm.geosword.activities.ModifyAIActivity.ai_3_7", "5"));
                                                     ships[i].aislidervalues[8] = Integer.decode(sp.getString("com.lloydm.geosword.activities.ModifyAIActivity.ai_3_8", "5"));
                                                     ships[i].aislidervalues[9] = Integer.decode(sp.getString("com.lloydm.geosword.activities.ModifyAIActivity.ai_3_9", "5"));
                                                     ships[i].aislidervalues[10] = Integer.decode(sp.getString("com.lloydm.geosword.activities.ModifyAIActivity.ai_3_10", "5"));
                                                     ships[i].aislidervalues[11] = Integer.decode(sp.getString("com.lloydm.geosword.activities.ModifyAIActivity.ai_3_11", "5"));
                                                     ships[i].aislidervalues[12] = Integer.decode(sp.getString("com.lloydm.geosword.activities.ModifyAIActivity.ai_3_12", "5"));
                                                     ships[i].aislidervalues[13] = Integer.decode(sp.getString("com.lloydm.geosword.activities.ModifyAIActivity.ai_3_13", "5"));
                                                     ships[i].aislidervalues[14] = Integer.decode(sp.getString("com.lloydm.geosword.activities.ModifyAIActivity.ai_3_14", "5"));
                                                     ships[i].aislidervalues[15] = Integer.decode(sp.getString("com.lloydm.geosword.activities.ModifyAIActivity.ai_3_15", "5"));
                                     }
                                     if (i > 7)
                                     {
                                                     ships[i].aislidervalues[0] = Integer.decode(sp.getString("com.lloydm.geosword.activities.ModifyAIActivity.ai_4_0", "5"));
                                                     ships[i].aislidervalues[1] = Integer.decode(sp.getString("com.lloydm.geosword.activities.ModifyAIActivity.ai_4_1", "5"));
                                                     ships[i].aislidervalues[2] = Integer.decode(sp.getString("com.lloydm.geosword.activities.ModifyAIActivity.ai_4_2", "5"));
                                                     ships[i].aislidervalues[3] = Integer.decode(sp.getString("com.lloydm.geosword.activities.ModifyAIActivity.ai_4_3", "5"));
                                                     ships[i].aislidervalues[4] = Integer.decode(sp.getString("com.lloydm.geosword.activities.ModifyAIActivity.ai_4_4", "5"));
                                                     ships[i].aislidervalues[5] = Integer.decode(sp.getString("com.lloydm.geosword.activities.ModifyAIActivity.ai_4_5", "5"));
                                                     ships[i].aislidervalues[6] = Integer.decode(sp.getString("com.lloydm.geosword.activities.ModifyAIActivity.ai_4_6", "5"));
                                                     ships[i].aislidervalues[7] = Integer.decode(sp.getString("com.lloydm.geosword.activities.ModifyAIActivity.ai_4_7", "5"));
                                                     ships[i].aislidervalues[8] = Integer.decode(sp.getString("com.lloydm.geosword.activities.ModifyAIActivity.ai_4_8", "5"));
                                                     ships[i].aislidervalues[9] = Integer.decode(sp.getString("com.lloydm.geosword.activities.ModifyAIActivity.ai_4_9", "5"));
                                                     ships[i].aislidervalues[10] = Integer.decode(sp.getString("com.lloydm.geosword.activities.ModifyAIActivity.ai_4_10", "5"));
                                                     ships[i].aislidervalues[11] = Integer.decode(sp.getString("com.lloydm.geosword.activities.ModifyAIActivity.ai_4_11", "5"));
                                                     ships[i].aislidervalues[12] = Integer.decode(sp.getString("com.lloydm.geosword.activities.ModifyAIActivity.ai_4_12", "5"));
                                                     ships[i].aislidervalues[13] = Integer.decode(sp.getString("com.lloydm.geosword.activities.ModifyAIActivity.ai_4_13", "5"));
                                                     ships[i].aislidervalues[14] = Integer.decode(sp.getString("com.lloydm.geosword.activities.ModifyAIActivity.ai_4_14", "5"));
                                                     ships[i].aislidervalues[15] = Integer.decode(sp.getString("com.lloydm.geosword.activities.ModifyAIActivity.ai_4_15", "5"));
                                     }
                     }
     }
}