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
                                                                                                                     {
                                                      &n