package com.lloydm.geosword;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.util.Random;

import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.util.Log;

import com.threed.jpct.Camera;
import com.threed.jpct.FrameBuffer;
import com.threed.jpct.GLSLShader;
import com.threed.jpct.Logger;
import com.threed.jpct.Matrix;
import com.threed.jpct.Object3D;
import com.threed.jpct.SimpleVector;
import com.threed.jpct.Texture;
import com.threed.jpct.TextureManager;
import com.threed.jpct.World;
import com.lloydm.geosword.common.CameraKeyFrame;
import com.lloydm.geosword.common.Config;
import com.lloydm.geosword.common.HardPoint;
import com.lloydm.geosword.common.Weapon;
import com.lloydm.geosword.common.KeyFrame;
import com.lloydm.geosword.common.KeyFrameData;
import com.lloydm.geosword.common.Ship;
import com.lloydm.geosword.PilotChatter;

public class Level3
{
     // shader version.....
     // also uses new ship models.
     // each iteration of "level" class is an update......
     // also now loads replay from file...while testing....still very early.....
     private final static String TAG = "com.lloydm.geosword.Level3";
     private final static String TIMERTAG = "com.lloydm.geosword.Timing";

     public final static SimpleVector ROTATE3DS = new SimpleVector(0, -1, 0);

     public final static SimpleVector laservert1 = new SimpleVector(-1.0f, 0, 0);
     public final static SimpleVector laservert2 = new SimpleVector(1.0f, 0, 0);
     public final static SimpleVector laservert3 = new SimpleVector(1.0f, 0, 15f);
     public final static SimpleVector laservert4 = new SimpleVector(-1.0f, 0, 15f);

     public final static SimpleVector beamvert1 = new SimpleVector(-20.5f, 0, 0);
     public final static SimpleVector beamvert2 = new SimpleVector(20.5f, 0, 0);
     public final static SimpleVector beamvert3 = new SimpleVector(20.5f, 0, 2000f);
     public final static SimpleVector beamvert4 = new SimpleVector(-20.5f, 0, 2000f);

     public final static SimpleVector beamvert5 = new SimpleVector(0, -20.5f, 0);
     public final static SimpleVector beamvert6 = new SimpleVector(0, 20.5f, 0);
     public final static SimpleVector beamvert7 = new SimpleVector(0, 20.5f, 2000f);
     public final static SimpleVector beamvert8 = new SimpleVector(0f, -20.5f, 2000f);

     public Texture cachetex = null;

     public volatile int skipnum = 0;

     public PilotChatter pchatter = null;
     public volatile boolean musicflag = true;

     public volatile boolean failexit = false;
     public volatile boolean jumpoutimmediately = false;
     public volatile boolean safetoexit = false;
     // teams are:
     // red, blue, green, yellow, white, black, purple, orange

     public final static float TEAMRED[] = new float[] { 3f, 2.95f, 2.95f, 1.0f };
     // red

     public final static float TEAMBLUE[] = new float[] { 2.95f, 2.95f, 3f, 1.0f };
     // blue

     public final static float TEAMGREEN[] = new float[] { 2.95f, 3f, 2.95f, 1.0f };
     // green

     public final static float TEAMYELLOW[] = new float[] { 3f, 3f, 2.95f, 1.0f };
     // yellow

     public final static float TEAMWHITE[] = new float[] { 3f, 3f, 3f, 1.0f };
     // white

     public final static float TEAMBLACK[] = new float[] { 2.7f, 2.7f, 2.7f, 1.0f };
     // black

     public final static float TEAMPURPLE[] = new float[] { 3f, 2.95f, 3f, 1.0f };
     // purple

     public final static float TEAMORANGE[] = new float[] { 3f, 2.98f, 2.93f, 1.0f };
     // orange

     public final static float TEAMBLANK[] = new float[] { 1, 1, 1, 1 };
     // blank
     public final static int MAXRAND = 1000;
     public final static int MAXESPRITES = 200;
     public final static float ESPRITESIZE = 120;

     private static int LOWSPECDIV = 1;

     // new simple method for explosions.....
     public Texture explosiontextures[] = null;
     public Object3D esprites[] = null;
     public int espriteframe[] = null;
     public Matrix espritetexmatrix[] = null;

     //new simple method for engine trails for fighters and bombers......
     public final static int MAXENGSPRITES = (32)*2; //just do 32 x 2 and leave some of them blank.....wasted okay.....
     public final static float ENGSPRITESIZE = 8;
    
     public Texture enginetrailtextures[] = null;
     public Object3D enginesprites[] = null;
     public int enginespriteframe[] = null;
     public Matrix enginespritetexmatrix[] = null;
    
    
     public float erand[] = null;

     // hud elements (health for ships....)

     public Texture teamhptextures[] = null;
     public Object3D teamhpsprites[] = null;
     public Matrix teamhptexmatrix[] = null;
     public float teamhpucoord[] = null;
     public float teamhpvcoord[] = null;

     public volatile int ateami = -1;
     public volatile int dteami = -1;

     public float teamphpx[] = null; // x coordinates....

     // these are set in TestActivity AND only in TestActivity....
     // they are only READ in here......

     public volatile boolean setframe = false;
     public boolean settingframe = false;

     public volatile boolean showstats = false;

     public volatile float setframex = -1f;
     public volatile float setframey = -1f;

     public int fwidth = -1;
     public int fheight = -1;

     public float timepercent = -1;

     public int setframenumber = -1;

     public int currentframe;
     public int newframe = -1;
     public int catchupframe = -1;

     public boolean viewmode = false;
     public volatile boolean levelloaded = false;
     public volatile boolean loading = false;
     public volatile int loadvalue = 0;
     public volatile int lprog = 0;

     private boolean gpuready = false;
     private int gpureadycount = 0;

     public KeyFrame[] keyframe = new KeyFrame[8000];
     private CameraKeyFrame[] cameraframe = new CameraKeyFrame[8000];
     public int[] irandkeys = new int[8000];
     public int maxframes;

     public int splitindex = -1;

     public Mesh[] mesh;

     public Object3D loadingscreen = null;

     public LaserBeam[] laserbeams = new LaserBeam[1000];
     public Ship[] ships = new Ship[32];
     public SpaceDebris[] spacedebris;
     // public Explosion explosions = null;

     public World world;
     public Camera camera;
     public FrameBuffer fbuffer;
     public AssetManager assets;
     public GLSLShader[] shaders;
     public String rfile = null;

     public int irand;
     public Random rand;

     // temp variables changed from local to class members....basically so we
     // don't keep creating stuff....
     public SimpleVector pos, adjust, target, oldt, spawnpos, screenvec;
     public Matrix adjustmatrix;
     public float angle1, angle2;
     public boolean tmpboolean = false, smoking = false,
                                     massiveexplosion = false;
     public int size, i, j, k;
     public int tmpi;
     public float camx = 0, camy = 0, camz = 0, camdx = 0, camdy = 0, camdz = 1;
     public float mdx = 0, mdy = 0, mdz = 0, mlen = 0;
     public float bvx = 0, bvy = 0, bvz = 0, blen = 0;

     public Level3(final int levelnumber, final World world, AssetManager assets, final GLSLShader[] shaders, final String replayfile, final boolean viewmode, final boolean musicflag)
     {
                     if (loading)
                     {
                                     return;
                     }
                     this.viewmode = viewmode;
                     this.assets = assets;
                     this.shaders = shaders;
                     this.rfile = replayfile;
                     this.musicflag = musicflag;
                     if (Config.ISLOWSPECDEVICE)
                     {
                                     LOWSPECDIV = 2;
                     }
                     Log.i(TAG, "Rfile in Level3:" + this.rfile);

                     new Thread(new Runnable()
                     {

                                     @Override
                                     public void
run()
                                     {
                                                     failexit = false;
                                                     Log.i(TIMERTAG, "TIME:(START LOAD):" + System.currentTimeMillis());
                                                     loading = true;
                                                     levelloaded = false;
                                                     loadvalue = 25;
                                                     logmemoryusage();
                                                     lprog = 0;
                                                     loadlevel(world);
                                                     logmemoryusage();
                                                     loadvalue = 50;
                                                     levelloaded = true;
                                                     loading = false;
                                                     lprog = 1;
                                                     for (i = 0; i < 8000; i++)
                                                     {
                                                                     irandkeys[i] = -1;
                                                     }
                                                     Log.i(TIMERTAG, "TIME:(END LOAD):" + System.currentTimeMillis());
                                                     if (jumpoutimmediately)
                                                     {
                                                                     safetoexit = true;
                                                     }
                                                     System.gc();
                                     }
                     }).start();

     }

     public void update()
     {
                     if (loading)
                     {
                                     return;
                     }
                     if (rand == null)
                     {
                                     rand = new Random();
                                     rand.setSeed(System.currentTimeMillis());
                     }
                     if (erand == null)
                     {
                                     erand = new float[MAXRAND / LOWSPECDIV];
                                     for (i = 0; i < MAXRAND / LOWSPECDIV; i++)
                                     {
                                                     erand[i] = (rand.nextFloat() - 0.5f) * 2.0f;
                                     }
                                     if (lprog == 1)
                                     {
                                                     lprog = 2;
                                     }
                     }

                     if (currentframe < 8000 && currentframe >= 0)
                     {
                                     irandkeys[currentframe] = irand;
                     }

                     if (currentframe == 1)
                     {
                                     Log.i(TIMERTAG, "TIME:(GAME SHOULD HAVE STARTED FRAME:1:" + System.currentTimeMillis());
                                     if (lprog < 99)
                                     {
                                                     lprog = 99;
                                     }
                     }
                     if (currentframe == 4000)
                     {
                                     Log.i(TAG, "TIME:(AT FRAME 4000):" + System.currentTimeMillis());

                     }
                     if (currentframe == 0)
                     {
                                     Log.d(TAG, "Check GC from here");
                                     for (i = 0; i < 32; i++)
                                     {
                                                     if (ships[i] == null)
                                                     {
                                                                     continue;
                                                     }
                                                     if (ships[i].shiptype < 3)
                                                     {
                                                                     // ships[i].mesh[0].setRenderHook(new BeamSplitRenderHook());

                                                                     // we need to make two copies of the original mesh....
                                                                     // which are hidden at all times....
                                                                     // currently going to split them down the middle.....see if it works.....it may.....
                                                                     // do this after work most likely....

                                                                     /*
                                                                         * if (ships[i].meshhalfA == null) { // ships[i].meshhalfA = ships[i].mesh[0].cloneObject(); // ships[i].meshhalfA.setTransparency(255); //
                                                                         * ships[i].meshhalfA.setTransparencyMode(Object3D.TRANSPARENCY_MODE_DEFAULT);
                                                                         *
                                                                         * } if (ships[i].meshhalfB == null) { // ships[i].meshhalfB = ships[i].mesh[0].cloneObject(); // ships[i].meshhalfB.setTransparency(255); //
                                                                         * ships[i].meshhalfB.setTransparencyMode(Object3D.TRANSPARENCY_MODE_DEFAULT); } if (!gpuready) { if (ships[i].aa != null) { Log.i(TAG, "setting shader7");
                                                                         * ships[i].aa.setCurrentShader(this.shaders[7]); ships[i].aa.myshader = this.shaders[7]; ships[i].meshhalfA.setRenderHook(ships[i].aa);
                                                                         * ships[i].meshhalfA.setShader(this.shaders[7]);
                                                                         *
                                                                         * } if (ships[i].bb != null) { Log.i(TAG, "setting shader7"); ships[i].bb.setCurrentShader(this.shaders[7]); ships[i].bb.myshader = this.shaders[7];
                                                                         * ships[i].meshhalfB.setRenderHook(ships[i].bb); ships[i].meshhalfB.setShader(this.shaders[7]); } }
                                                                         *
/
                                                                     ships[i].hh = new HighlightRenderHook();
                                                                     ships[i].hh.setCurrentShader(this.shaders[2]);
                                                                     ships[i].hh.myshader = this.shaders[2];
                                                                     ships[i].hh.highlighton = false;
                                                                     ships[i].hh.ff = new float[4];
                                                                     ships[i].mesh[0].setRenderHook(ships[i].hh);
                                                                     ships[i].mesh[0].setShader(this.shaders[2]);
                                                     }
                                                     else
                                                     { // small ships get different shaders (slightly no bump mapping
                                                             // but keep specular)
                                                                     ships[i].hh = new HighlightRenderHook();
                                                                     ships[i].hh.setCurrentShader(this.shaders[2]);
                                                                     ships[i].hh.myshader = this.shaders[2];
                                                                     ships[i].hh.highlighton = false;
                                                                     ships[i].hh.ff = new float[4];
                                                                     ships[i].mesh[0].setRenderHook(ships[i].hh);
                                                                     ships[i].mesh[0].setShader(this.shaders[2]);
                                                     }
                                     }
                                     Log.d(TAG, "Ship Shader set");
                     }
                     if (maxframes == 0)
                     {
                                     maxframes = keyframe.length;
                     }
                     // check if gpuready.....ie do caching here.......
                     if (gpuready == false)
                     {
                                     if (lprog < 5)
                                     {
                                                     lprog = 5;
                                     }
                                     loadvalue = 65;
                                     gpureadycount++;
                                     // set all ships to visible....
                                     if (camera != null)
                                     {
                                                     if (pos == null)
                                                     {
                                                                     pos = new SimpleVector(0, 0, 0);
                                                     }
                                                     if (spawnpos == null)
                                                     {
                                                                     spawnpos = new SimpleVector(0, 0, 0);
                                                     }
                                                     pos.x = 0;
                                                     pos.y = 0;
                                                     pos.z = 0;
                                                     spawnpos.x = 0;
                                                     spawnpos.y = 0;
                                                     spawnpos.z = 500;

                                                     camera.setPosition(pos);
                                                     camera.lookAt(spawnpos);

                                     }
                                     for (i = 0; i < 32; i++)
                                     {
                                                     if (ships[i] != null)
                                                     {
                                                                     if (ships[i].mesh != null)
                                                                     {
                                                                                     if (ships[i].mesh[0] != null)
                                                                                     {
                                                                                                     ships[i].mesh[0].setVisibility(true);

                                                                                                     if (ships[i].wmatrix == null)
                                                                                                     {
                                                                                                                     ships[i].wmatrix = ships[i].pivot.getWorldTransformation();
                                                                                                     }
                                                                                                     else
                                                                                                     {
                                                                                                                     ships[i].wmatrix = ships[i].pivot.getWorldTransformation(ships[i].wmatrix);
                                                                                                     }
                                                                                                     if (pos == null)
                                                                                                     {
                                                                                                                     pos = ships[i].wmatrix.getTranslation();
                                                                                                     }
                                                                                                     pos = ships[i].wmatrix.getTranslation(pos);
                                                                                                     pos.scalarMul(-1);
                                                                                                     if (!viewmode)
                                                                                                     {
                                                                                                                     ships[i].pivot.translate(pos);
                                                                                                                     ships[i].pivot.translate(0, 0, 500);
                                                                                                     }
                                                                                     }
                                                                                     /*
                                                                                         * if (ships[i].meshhalfA != null) { ships[i].meshhalfA.setVisibility(true); } if (ships[i].meshhalfB != null) { ships[i].meshhalfB.setVisibility(true); }
                                                                                         *
/
                                                                     }
                                                     }
                                     }
                                     if (lprog < 6)
                                     {
                                                     lprog = 6;
                                     }
                                     for (i = 0; i < 32; i++)
                                     {
                                                     if (teamhpsprites != null)
                                                     {
                                                                     if (teamhpsprites[i] != null)
                                                                     {
                                                                                     teamhpsprites[i].setVisibility(true);
                                                                                     if (adjustmatrix == null)
                                                                                     {
                                                                                                     adjustmatrix = teamhpsprites[i].getWorldTransformation();
                                                                                     }
                                                                                     else
                                                                                     {
                                                                                                     adjustmatrix = teamhpsprites[i].getWorldTransformation(adjustmatrix);
                                                                                     }
                                                                                     if (oldt == null)
                                                                                     {
                                                                                                     oldt = adjustmatrix.getTranslation();
                                                                                     }
                                                                                     else
                                                                                     {
                                                                                                     oldt = adjustmatrix.getTranslation(oldt);
                                                                                     }
                                                                                     oldt.scalarMul(-1f);
                                                                     }
                                                     }
                                     }
                                     if (lprog < 7)
                                     {
                                                     lprog = 7;
                                     }
                                     for (i = 0; i < MAXESPRITES / LOWSPECDIV; i++)
                                     {
                                                     if (esprites != null)
                                                     {
                                                                     if (esprites[i] != null)
                                                                     {
                                                                                     esprites[i].setVisibility(true);
                                                                                     if (adjustmatrix == null)
                                                                                     {
                                                                                                     adjustmatrix = esprites[i].getWorldTransformation();
                                                                                     }
                                                                                     else
                                                                                     {
                                                                                                     adjustmatrix = esprites[i].getWorldTransformation(adjustmatrix);
                                                                                     }
                                                                                     if (oldt == null)
                                                                                     {
                                                                                                     oldt = adjustmatrix.getTranslation();
                                                                                     }
                                                                                     else
                                                                                     {
                                                                                                     oldt = adjustmatrix.getTranslation(oldt);
                                                                                     }
                                                                                     oldt.scalarMul(-1f);
                                                                                     esprites[i].translate(oldt);
                                                                                     esprites[i].setScale(1.0f);
                                                                                     esprites[i].translate(0, 0, 500);
                                                                     }
                                                     }
                                     }
                                     for(i=0;i<MAXENGSPRITES;i++)
                                     {
                                                     if(enginesprites!=null)
                                                     {
                                                                     if(enginesprites[i]!=null)
                                                                     {
                                                                                     enginesprites[i].setVisibility(true);
                                                                                     if(adjustmatrix == null)
                                                                                     {
                                                                                                     adjustmatrix = enginesprites[i].getWorldTransformation();
                                                                                     }
                                                                                     else
                                                                                     {
                                                                                                     adjustmatrix = enginesprites[i].getWorldTransformation(adjustmatrix);
                                                                                     }
                                                                                     if(oldt == null)
                                                                                     {
                                                                                                     oldt = adjustmatrix.getTranslation();
                                                                                     }
                                                                                     else
                                                                                     {
                                                                                                     oldt = adjustmatrix.getTranslation(oldt);
                                                                                     }
                                                                                     oldt.scalarMul(-1f);
                                                                                     enginesprites[i].translate(oldt);
                                                                                     enginesprites[i].setScale(1.0f);
                                                                                     enginesprites[i].translate(0,0,500);
                                                                     }
                                                     }
                                     }
                                     if (lprog < 8)
                                     {
                                                     lprog = 8;
                                     }
                                     gpureadycount++;
                                     if (gpureadycount > 20)
                                     {
                                                     // hide everything again.......
                                                     if (!viewmode)
                                                     {
                                                                     for (i = 0; i < 32; i++)
                                                                     {
                                                                                     if (ships[i] != null)
                                                                                     {
                                                                                                     if (ships[i].mesh != null)
                                                                                                     {
                                                                                                                     if (ships[i].mesh[0] != null)
                                                                                                                     {
                                                                                                                                     ships[i].mesh[0].setVisibility(false);
                                                                                                                     }
                                                                                                                     /*
                                                                                                                         * if (ships[i].meshhalfA != null) { ships[i].meshhalfA.setVisibility(false); } if (ships[i].meshhalfB != null) { ships[i].meshhalfB.setVisibility(false); }
                                                                                                                         *
/
                                                                                                     }
                                                                                     }
                                                                     }
                                                     }
                                                     for (i = 0; i < 32; i++)
                                                     {
                                                                     if (teamhpsprites != null)
                                                                     {
                                                                                     if (teamhpsprites[i] != null)
                                                                                     {
                                                                                                     teamhpsprites[i].setVisibility(false);
                                                                                     }
                                                                     }
                                                     }

                                                     for (i = 0; i < MAXESPRITES / LOWSPECDIV; i++)
                                                     {
                                                                     if (esprites != null)
                                                                     {
                                                                                     if (esprites[i] != null)
                                                                                     {
                                                                                                     esprites[i].setVisibility(false);
                                                                                     }
                                                                     }
                                                     }
                                                     for(i = 0;i<MAXENGSPRITES;i++)
                                                     {
                                                                     if(enginesprites!=null)
                                                                     {
                                                                                     if(enginesprites[i]!=null)
                                                                                     {
                                                                                                     enginesprites[i].setVisibility(false);
                                                                                     }
                                                                     }
                                                     }
                                                    
                                                     if (loadingscreen != null)
                                                     {
                                                                     gpuready = true;
                                                                     Log.d(TAG, "gpu now ready");
                                                                     logmemoryusage();
                                                                     loadvalue = 95;
                                                                     loadingscreen.setVisibility(false);
                                                     }
                                                     if (viewmode)
                                                     {
                                                                     gpuready = true;
                                                                     Log.d(TAG, "gpu now ready");
                                                                     logmemoryusage();
                                                     }
                                     }
                                     if (lprog < 12)
                                     {
                                                     lprog = 12;
                                     }
                                     return;
                     }
                     // gpu is ready......

                     // this section is for updating the replay time position (seeking pos)
                     // based on user interaction with slider
                     if (setframe == true && settingframe == false)
                     {
                                     if (setframex > 0) // && setframey>0)
                                     {
                                                     // height of screen is?

                                                     if (fbuffer != null)
                                                     {
                                                                     if (fwidth == -1)
                                                                     {
                                                                                     fwidth = fbuffer.getWidth();
                                                                     }
                                                                     if (fheight == -1)
                                                                     {
                                                                                     fheight = fbuffer.getHeight();
                                                                     }
                                                                     if (fwidth != -1 && fheight != -1)
                                                                     {
                                                                                     // check relative position of touchx.y
                                                                                     // we will have a slider showing eventually but for now
                                                                                     // simply do the calcs.....

                                                                                     // if(setframey > 0.8f*fheight)
                                                                                     {
                                                                                                     // if(setframex>0.05f*fwidth &&
                                                                                                     // setframex<0.95f*fwidth)
                                                                                                     {
                                                                                                                     // this is where slider is.....
                                                                                                                     if (fwidth > 0f)
                                                                                                                     {
                                                                                                                                     settingframe = true;
                                                                                                                                     timepercent = (setframex / 8002f); // (setframex
                                                                                                                                                                                                                                                                                 // -
                                                                                                                                                                                                                                                                                 // 0.05f*fwidth)/
                                                                                                                                                                                                                                                                                 // (0.9f*fwidth);
                                                                                                                                     newframe = (int) ((float) maxframes * timepercent) - 150;
                                                                                                                                     // reset all bullets and esprites...effects
                                                                                                                                     // etc.....

                                                                                                                                     for (j = 0; j < 1000; j++)
                                                                                                                                     {
                                                                                                                                                     if (laserbeams[j] != null)
                                                                                                                                                     {
                                                                                                                                                                     if (laserbeams[j].lifetime > 0)
                                                                                                                                                                     {
                                                                                                                                                                                     laserbeams[j].lifetime = 0;
                                                                                                                                                                                     // if (laserbeams[j].isbeam)
                                                                                                                                                                                     // {
                                                                                                                                                                                     laserbeams[j].beammesh.setVisibility(false);
                                                                                                                                                                                     // }
                                                                                                                                                                                     // else
                                                                                                                                                                                     // {
                                                                                                                                                                                     laserbeams[j].mesh.setVisibility(false);
                                                                                                                                                                                     // }
                                                                                                                                                                     }
                                                                                                                                                                     else
                                                                                                                                                                     {
                                                                                                                                                                                     laserbeams[j].beammesh.setVisibility(false);
                                                                                                                                                                                     laserbeams[j].mesh.setVisibility(false);
                                                                                                                                                                     }
                                                                                                                                                     }
                                                                                                                                                     else
                                                                                                                                                     {
                                                                                                                                                                     break;
                                                                                                                                                     }
                                                                                                                                     }
                                                                                                                                     for (j = 0; j < 65; j++)
                                                                                                                                     {
                                                                                                                                                     if (esprites != null)
                                                                                                                                                     {
                                                                                                                                                                     k = 0;
                                                                                                                                                                     for (i = 0; i < MAXESPRITES / LOWSPECDIV; i++)
                                                                                                                                                                     {
                                                                                                                                                                                     if (esprites[i] != null)
                                                                                                                                                                                     {
                                                                                                                                                                                                     esprites[i].setVisibility(false);
                                                                                                                                                                                                     if (espriteframe[i] == -1)
                                                                                                                                                                                                     {
                                                                                                                                                                                                                     continue;
                                                                                                                                                                                                     }
                                                                                                                                                                                                     k = 1;
                                                                                                                                                                                                     // change texture coords of
                                                                                                                                                                                                     // the sprite.....
                                                                                                                                                                                                     if (espriteframe[i] >= 64)
                                                                                                                                                                                                     {
                                                                                                                                                                                                                     espriteframe[i] = -1;
                                                                                                                                                                                                                     espritetexmatrix[i].translate(-0.875f, -0.875f, 0f);
                                                                                                                                                                                                                     esprites[i].setTextureMatrix(espritetexmatrix[i]);
                                                                                                                                                                                                                     esprites[i].touch();
                                                                                                                                                                                                     }
                                                                                                                                                                                                     else
                                                                                                                                                                                                     {
                                                                                                                                                                                                                     if (espriteframe[i] % 8 == 0)
                                                                                                                                                                                                                     {
                                                                                                                                                                                                                                     if (espriteframe[i] != 0)
                                                                                                                                                                                                                                     {
                                                                                                                                                                                                                                                     espritetexmatrix[i].translate(-0.875f, 0.125f, 0f);
                                                                                                                                                                                                                                                     esprites[i].setTextureMatrix(espritetexmatrix[i]);
                                                                                                                                                                                                                                                     esprites[i].touch();
                                                                                                                                                                                                                                     }
                                                                                                                                                                                                                     }
                                                                                                                                                                                                                     else
                                                                                                                                                                                                                     {
                                                                                                                                                                                                                                     espritetexmatrix[i].translate(0.125f, 0f, 0f);
                                                                                                                                                                                                                                     esprites[i].setTextureMatrix(espritetexmatrix[i]);
                                                                                                                                                                                                                                     esprites[i].touch();
                                                                                                                                                                                                                     }
                                                                                                                                                                                                                     espriteframe[i]++;
                                                                                                                                                                                                     }
                                                                                                                                                                                     }
                                                                                                                                                                     }
                                                                                                                                                                     if (k == 0)
                                                                                                                                                                     {
                                                                                                                                                                                     break;
                                                                                                                                                                     }
                                                                                                                                                     }
                                                                                                                                     }

                                                                                                                                     if (newframe < 0)
                                                                                                                                     {
                                                                                                                                                     newframe = 0;
                                                                                                                                     }
                                                                                                                                     if (newframe > maxframes - 1)
                                                                                                                                     {
                                                                                                                                                     newframe = maxframes - 1;
                                                                                                                                     }
                                                                                                                                     // may or may not work..should though....
                                                                                                                                     if (newframe >= 0 && newframe < 8000)
                                                                                                                                     {
                                                                                                                                                     if (irandkeys[newframe] != -1)
                                                                                                                                                     {
                                                                                                                                                                     irand = irandkeys[newframe];
                                                                                                                                                     }
                                                                                                                                     }

                                                                                                                                     catchupframe = newframe - 200;
                                                                                                                                     for (currentframe = catchupframe; currentframe < newframe; currentframe++)
                                                                                                                                     {
                                                                                                                                                     // only things we really care about are
                                                                                                                                                     // the explosions and lasers....
                                                                                                                                                     // nothing else makes any difference
                                                                                                                                                     // whatsoever......
                                                                                                                                                     // we also care about the split meshes....
                                                                                                                                                     if (currentframe > 0 && currentframe < maxframes)
                                                                                                                                                     {
                                                                                                                                                                     if (irandkeys[currentframe] == -1)
                                                                                                                                                                     {
                                                                                                                                                                                     irandkeys[currentframe] = irand;
                                                                                                                                                                     }
                                                                                                                                                                     // /////////////////////////////DUPLICATE
                                                                                                                                                                     // OF BELOW CODE
                                                                                                                                                                     for (i = 0; i < 32; i++)
                                                                                                                                                                     {

                                                                                                                                                                                     if (showstats)
                                                                                                                                                                                     {
                                                                                                                                                                                                     ships[i].hh.highlighton = true;
                                                                                                                                                                                                     if (ships[i].team == 1)
                                                                                                                                                                                                     {
                                                                                                                                                                                                                     ships[i].hh.ff[0] = 2f;
                                                                                                                                                                                                                     ships[i].hh.ff[1] = 0f;
                                                                                                                                                                                                                     ships[i].hh.ff[2] = 0f;

                                                                                                                                                                                                     }
                                                                                                                                                                                                     if (ships[i].team == 2)
                                                                                                                                                                                                     {
                                                                                                                                                                                                                     ships[i].hh.ff[2] = 2f;
                                                                                                                                                                                                                     ships[i].hh.ff[1] = 2f;
                                                                                                                                                                                                                     ships[i].hh.ff[0] = 0f;
                                                                                                                                                                                                     }
                                                                                                                                                                                                     if (ships[i].team == 3)
                                                                                                                                                                                                     {
                                                                                                                                                                                                                     ships[i].hh.ff[0] = 2f;
                                                                                                                                                                                                                     ships[i].hh.ff[1] = 2f;
                                                                                                                                                                                                                     ships[i].hh.ff[2] = 0f;
                                                                                                                                                                                                     }
                                                                                                                                                                                                     if (ships[i].team == 4)
                                                                                                                                                                                                     {
                                                                                                                                                                                                                     ships[i].hh.ff[1] = 2f;
                                                                                                                                                                                                                     ships[i].hh.ff[0] = 0f;
                                                                                                                                                                                                                     ships[i].hh.ff[2] = 0f;
                                                                                                                                                                                                     }
                                                                                                                                                                                                     if (ships[i].team == 5)
                                                                                                                                                                                                     {
                                                                                                                                                                                                                     ships[i].hh.ff[0] = 2f;
                                                                                                                                                                                                                     ships[i].hh.ff[1] = 0f;
                                                                                                                                                                                                                     ships[i].hh.ff[2] = 2f;
                                                                                                                                                                                                     }
                                                                                                                                                                                                     ships[i].hh.ff[3] = (float) keyframe[currentframe].shipdata[i].hp / (float) ships[i].fullhp;
                                                                                                                                                                                     }
                                                                                                                                                                                     else
                                                                                                                                                                                     {
                                                                                                                                                                                                     ships[i].hh.highlighton = false;
                                                                                                                                                                                     }

                                                                                                                                                                                     tmpboolean = false;
                                                                                                                                                                                     smoking = false;
                                                                                                                                                                                     massiveexplosion = false;
                                                                                                                                                                                     if (currentframe + 50 < maxframes && ships[i].shiptype < 3)
                                                                                                                                                                                     {
                                                                                                                                                                                                     if (keyframe[currentframe + 50].shipdata[i].hp <= 0 && keyframe[currentframe].shipdata[i].hp < ships[i].fullhp && keyframe[currentframe].shipdata[i].hp > 0 && keyframe[currentframe + 50].shipdata[i].hp > -10000 && ships[i].splitframe <= 0)
                                                                                                                                                                                                     {
                                                                                                                                                                                                                     if (currentframe % 10 == 0)
                                                                                                                                                                                                                     {
                                                                                                                                                                                                                                     tmpboolean = true;
                                                                                                                                                                                                                     }
                                                                                                                                                                                                     }
                                                                                                                                                                                                     if (currentframe - ships[i].splitframe < 50 && currentframe - ships[i].splitframe > -25 && currentframe % 10 == 0 && ships[i].splitframe > 0)
                                                                                                                                                                                                     {
                                                                                                                                                                                                                     tmpboolean = true;
                                                                                                                                                                                                     }
                                                                                                                                                                                                     // added
                                                                                                                                                                                                     if (keyframe[currentframe + 10].shipdata[i].hp <= 0 && keyframe[currentframe + 9].shipdata[i].hp > 0)
                                                                                                                                                                                                     {
                                                                                                                                                                                                                     massiveexplosion = true;
                                                                                                                                                                                                     }
                                                                                                                                                                                     }
                                                                                                                                                                                     if (currentframe + 200 < maxframes && ships[i].shiptype > 2 && keyframe[currentframe].shipdata[i].hp > 0 && keyframe[currentframe].shipdata[i].hp < ships[i].fullhp)
                                                                                                                                                                                     {
                                                                                                                                                                                                     if (currentframe % 3 == 0 && keyframe[currentframe + 200].shipdata[i].hp <= 0)
                                                                                                                                                                                                     {
                                                                                                                                                                                                                     smoking = true;
                                                                                                                                                                                                                     tmpboolean = true;
                                                                                                                                                                                                     }

                                                                                                                                                                                     }
                                                                                                                                                                                     if (currentframe + 50 < maxframes && ships[i].shiptype > 2)
                                                                                                                                                                                     {
                                                                                                                                                                                                     if (keyframe[currentframe + 10].shipdata[i].hp <= 0 && keyframe[currentframe + 9].shipdata[i].hp > 0)
                                                                                                                                                                                                     {
                                                                                                                                                                                                                     massiveexplosion = true;
                                                                                                                                                                                                     }
                                                                                                                                                                                     }
                                                                                                                                                                                     angle2 = 1;
                                                                                                                                                                                     if (currentframe + 16 < maxframes)
                                                                                                                                                                                     {
                                                                                                                                                                                                     if (keyframe[currentframe + 14].shipdata[i].hp > 0 && keyframe[currentframe + 15].shipdata[i].hp <= 0)
                                                                                                                                                                                                     {
                                                                                                                                                                                                                     tmpboolean = true;
                                                                                                                                                                                                                     angle2 = 0;
                                                                                                                                                                                                     }
                                                                                                                                                                                     }
                                                                                                                                                                                     if (keyframe[currentframe].shipdata[i].hp != keyframe[currentframe - 1].shipdata[i].hp || tmpboolean || massiveexplosion)
                                                                                                                                                                                     {
                                                                                                                                                                                                     // put explosion....
                                                                                                                                                                                                     // simply update the
                                                                                                                                                                                                     // position of the relevant
                                                                                                                                                                                                     // esprite.....find one that
                                                                                                                                                                                                     // has a -1 value....
                                                                                                                                                                                                     if (esprites != null && ((currentframe % 2 == 0 && ships[i].shiptype > 2) || (angle2 == 0) || (currentframe % 6 == 0 && ships[i].shiptype < 3) || massiveexplosion))
                                                                                                                                                                                                     {
                                                                                                                                                                                                                     for (j = 0; j < MAXESPRITES / LOWSPECDIV; j++)
                                                                                                                                                                                                                     {
                                                                                                                                                                                                                                     if (espriteframe[j] == -1)
                                                                                                                                                                                                                                     {
                                                                                                                                                                                                                                                     espriteframe[j] = 0;
                                                                                                                                                                                                                                                     if (adjustmatrix == null)
                                                                                                                                                                                                                                                     {
                                                                                                                                                                                                                                                                     adjustmatrix = esprites[j].getWorldTransformation();
                                                                                                                                                                                                                                                     }
                                                                                                                                                                                                                                                     else
                                                                                                                                                                                                                                                     {
                                                                                                                                                                                                                                                                     adjustmatrix = esprites[j].getWorldTransformation(adjustmatrix);
                                                                                                                                                                                                                                                     }
                                                                                                                                                                                                                                                     if (oldt == null)
                                                                                                                                                                                                                                                     {
                                                                                                                                                                                                                                                                     oldt = adjustmatrix.getTranslation();
                                                                                                                                                                                                                                                     }
                                                                                                                                                                                                                                                     else
                                                                                                                                                                                                                                                     {
                                                                                                                                                                                                                                                                     oldt = adjustmatrix.getTranslation(oldt);
                                                                                                                                                                                                                                                     }
                                                                                                                                                                                                                                                     oldt.scalarMul(-1f);
                                                                                                                                                                                                                                                     esprites[j].translate(oldt);
                                                                                                                                                                                                                                                     esprites[j].setScale(1.0f);
                                                                                                                                                                                                                                                     // scale it up
                                                                                                                                                                                                                                                     // at this point
                                                                                                                                                                                                                                                     // based on size
                                                                                                                                                                                                                                                     // type.....
                                                                                                                                                                                                                                                     if (smoking)
                                                                                                                                                                                                                                                     {
                                                                                                                                                                                                                                                                     esprites[j].setScale(0.1f); // smoke
                                                                                                                                                                                                                                                                                                                                                                                     // trails......really
                                                                                                                                                                                                                                                                                                                                                                                     // small....
                                                                                                                                                                                                                                                     }
                                                                                                                                                                                                                                                     else
                                                                                                                                                                                                                                                     {
                                                                                                                                                                                                                                                                     if (massiveexplosion)
                                                                                                                                                                                                                                                                     {
                                                                                                                                                                                                                                                                                     esprites[j].setScale(2.5f + (ships[i].sizetype * 0.2f));
                                                                                                                                                                                                                                                                     }
                                                                                                                                                                                                                                                                     else
                                                                                                                                                                                                                                                                     {
                                                                                                                                                                                                                                                                                     if (currentframe + 30 < maxframes)
                                                                                                                                                                                                                                                                                     {
                                                                                                                                                                                                                                                                                                     if (keyframe[currentframe + 30].shipdata[i].hp <= 0 && keyframe[currentframe + 30].shipdata[i].hp > -10000)
                                                                                                                                                                                                                                                                                                     {
                                                                                                                                                                                                                                                                                                                     esprites[j].setScale(1.0f + (ships[i].sizetype * 0.05f));
                                                                                                                                                                                                                                                                                                     }
                                                                                                                                                                                                                                                                                                     else
                                                                                                                                                                                                                                                                                                     {
                                                                                                                                                                                                                                                                                                                     if (keyframe[currentframe + 7].shipdata[i].hp == -10000)
                                                                                                                                                                                                                                                                                                                     {
                                                                                                                                                                                                                                                                                                                                     esprites[j].setScale(0.5f + (ships[i].sizetype * 0.1f));
                                                                                                                                                                                                                                                                                                                     }
                                                                                                                                                                                                                                                                                                     }
                                                                                                                                                                                                                                                                                     }
                                                                                                                                                                                                                                                                     }
                                                                                                                                                                                                                                                     }

                                                                                                                                                                                                                                                     irand++;
                                                                                                                                                                                                                                                     irand %= MAXRAND / LOWSPECDIV;
                                                                                                                                                                                                                                                     if (!massiveexplosion)
                                                                                                                                                                                                                                                     {
                                                                                                                                                                                                                                                                     if (Math.abs(ships[i].splitframe - currentframe) < 150)
                                                                                                                                                                                                                                                                     {
                                                                                                                                                                                                                                                                                     // set scale to be quite small actually....
                                                                                                                                                                                                                                                                                     // and also set the angle1 to really small too
                                                                                                                                                                                                                                                                                     esprites[j].setScale(0.6f);
                                                                                                                                                                                                                                                                                     angle1 = 0.1f;
                                                                                                                                                                                                                                                                     }
                                                                                                                                                                                                                                                                     else
                                                                                                                                                                                                                                                                     {
                                                                                                                                                                                                                                                                                     angle1 = 1.0f;
                                                                                                                                                                                                                                                                     }
                                                                                                                                                                                                                                                                     esprites[j].translate(keyframe[currentframe].shipdata[i].x, keyframe[currentframe].shipdata[i].y, keyframe[currentframe].shipdata[i].z);
                                                                                                                                                                                                                                                                     esprites[j].translate(angle1 * 5.5f * (ships[i].sizetype + 1) * erand[irand], 0, 0);
                                                                                                                                                                                                                                                                     irand++;
                                                                                                                                                                                                                                                                     irand %= MAXRAND / LOWSPECDIV;
                                                                                                                                                                                                                                                                     esprites[j].translate(0, angle1 * 5.5f * (1 + ships[i].sizetype) * erand[irand], 0);
                                                                                                                                                                                                                                                                     irand++;
                                                                                                                                                                                                                                                                     irand %= MAXRAND / LOWSPECDIV;
                                                                                                                                                                                                                                                                     esprites[j].translate(0, 0, angle1 * 5.5f * (ships[i].sizetype + 1) * erand[irand]);
                                                                                                                                                                                                                                                     }
                                                                                                                                                                                                                                                     else
                                                                                                                                                                                                                                                     {
                                                                                                                                                                                                                                                                     esprites[j].translate(keyframe[currentframe].shipdata[i].x, keyframe[currentframe].shipdata[i].y, keyframe[currentframe].shipdata[i].z);
                                                                                                                                                                                                                                                     }
                                                                                                                                                                                                                                                     break;
                                                                                                                                                                                                                                     }
                                                                                                                                                                                                                     }
                                                                                                                                                                                                     }
                                                                                                                                                                                     }
                                                                                                                                                                                     size = keyframe[currentframe].shipdata[i].firingframe.length;
                                                                                                                                                                                     for (k = 0; k < size; k++)
                                                                                                                                                                                     {
                                                                                                                                                                                                     if (keyframe[currentframe].shipdata[i].firingframe[k] > 0)
                                                                                                                                                                                                     {
                                                                                                                                                                                                                     for (j = 0; j < 1000; j++)
                                                                                                                                                                                                                     {
                                                                                                                                                                                                                                     if (laserbeams[j] == null)
                                                                                                                                                                                                                                     {
                                                                                                                                                                                                                                                     if (pos == null)
                                                                                                                                                                                                                                                     {
                                                                                                                                                                                                                                                                     pos = new SimpleVector(0, 0, 0);
                                                                                                                                                                                                                                                     }
                                                                                                                                                                                                                                                     if (adjust == null)
                                                                                                                                                                                                                                                     {
                                                                                                                                                                                                                                                                     adjust = new SimpleVector(0, 0, 1);
                                                                                                                                                                                                                                                     }
                                                                                                                                                                                                                                                     if (adjustmatrix == null)
                                                                                                                                                                                                                                                     {
                                                                                                                                                                                                                                                                     adjustmatrix = adjust.getRotationMatrix();
                                                                                                                                                                                                                                                     }
                                                                                                                                                                                                                                                     if (target == null)
                                                                                                                                                                                                                                                     {
                                                                                                                                                                                                                                                                     target = new SimpleVector(0, 0, 1);
                                                                                                                                                                                                                                                     }
                                                                                                                                                                                                                                                     laserbeams[j] = new LaserBeam();
                                                                                                                                                                                                                                                     laserbeams[j].mesh = new Object3D(2);
                                                                                                                                                                                                                                                     // create
                                                                                                                                                                                                                                                     // mesh....
                                                                                                                                                                                                                                                     laserbeams[j].mesh.setCulling(false);
                                                                                                                                                                                                                                                     laserbeams[j].mesh.addTriangle(laservert1, 0f, 0f, laservert2, 1f, 0f, laservert3, 1f, 1f);
                                                                                                                                                                                                                                                     laserbeams[j].mesh.addTriangle(laservert3, 1f, 1f, laservert4, 0f, 1f, laservert1, 0f, 0f);

                                                                                                                                                                                                                                                     laserbeams[j].beammesh = new Object3D(4);
                                                                                                                                                                                                                                                     laserbeams[j].beammesh.setCulling(false);
                                                                                                                                                                                                                                                     laserbeams[j].beammesh.addTriangle(beamvert1, 0f, 0f, beamvert2, 1f, 0f, beamvert3, 1f, 1f);
                                                                                                                                                                                                                                                     laserbeams[j].beammesh.addTriangle(beamvert3, 1f, 1f, beamvert4, 0f, 1f, beamvert1, 0f, 0f);
                                                                                                                                                                                                                                                     laserbeams[j].beammesh.addTriangle(beamvert5, 0f, 0f, beamvert6, 1f, 0f, beamvert7, 1f, 1f);
                                                                                                                                                                                                                                                     laserbeams[j].beammesh.addTriangle(beamvert7, 1f, 1f, beamvert8, 0f, 1f, beamvert5, 0f, 0f);

                                                                                                                                                                                                                                                     laserbeams[j].scale = 1.0f;

                                                                                                                                                                                                                                                     if (ships[i].hardpoints[k].weapon.weapontype == 4) // plasma
                                                                                                                                                                                                                                                     {
                                                                                                                                                                                                                                                                     // laserbeams[j].beammesh.setTexture("plasma");
                                                                                                                                                                                                                                                                     // laserbeams[j].mesh.setTexture("plasma");
                                                                                                                                                                                                                                                                     laserbeams[j].scale = 4.0f;
                                                                                                                                                                                                                                                                     if (ships[i].shiptype < 3)
                                                                                                                                                                                                                                                                     {
                                                                                                                                                                                                                                                                                     laserbeams[j].scale = 8.0f;
                                                                                                                                                                                                                                                                     }
                                                                                                                                                                                                                                                     }
                                                                                                                                                                                                                                                     if (ships[i].hardpoints[k].weapon.weapontype == 5) // tractor
                                                                                                                                                                                                                                                     {
                                                                                                                                                                                                                                                                     // laserbeams[j].beammesh.setTexture("bluebeam");
                                                                                                                                                                                                                                                                     // laserbeams[j].mesh.setTexture("bluebeam");
                                                                                                                                                                                                                                                     }
                                                                                                                                                                                                                                                     if (ships[i].hardpoints[k].weapon.weapontype == 6) // disruptor
                                                                                                                                                                                                                                                     {
                                                                                                                                                                                                                                                                     // laserbeams[j].beammesh.setTexture("lgbeam"); // need
                                                                                                                                                                                                                                                                     // to
                                                                                                                                                                                                                                                                     // add
                                                                                                                                                                                                                                                                     // laserbeams[j].mesh.setTexture("lgbeam");
                                                                                                                                                                                                                                                                     if (ships[i].shiptype < 3 && ships[i].hardpoints[k].weapon.isbeam == false)
                                                                                                                                                                                                                                                                     {
                                                                                                                                                                                                                                                                                     laserbeams[j].scale = 6.0f;
                                                                                                                                                                                                                                                                     }
                                                                                                                                                                                                                                                     }
                                                                                                                                                                                                                                                     if (ships[i].hardpoints[k].weapon.weapontype == 7 || ships[i].hardpoints[k].weapon.weapontype == 3) // swarmer
                                                                                                                                                                                                                                                     {
                                                                                                                                                                                                                                                                     // laserbeams[j].beammesh.setTexture("yellowbeam"); // need
                                                                                                                                                                                                                                                                     // to
                                                                                                                                                                                                                                                                     // add
                                                                                                                                                                                                                                                                     // laserbeams[j].mesh.setTexture("yellowbeam"); // need
                                                                                                                                                                                                                                                                     // to
                                                                                                                                                                                                                                                                     // add
                                                                                                                                                                                                                                                                     if (ships[i].shiptype < 3)
                                                                                                                                                                                                                                                                     {
                                                                                                                                                                                                                                                                                     laserbeams[j].scale = 6.0f;
                                                                                                                                                                                                                                                                     }
                                                                                                                                                                                                                                                     }
                                                                                                                                                                                                                                                     if (ships[i].hardpoints[k].weapon.weapontype == 1) // standard
                                                                                                                                                                                                                                                                                                                                                                                                                                                                 // laser
                                                                                                                                                                                                                                                     {
                                                                                                                                                                                                                                                                     if (ships[i].shiptype < 3)
                                                                                                                                                                                                                                                                     {
                                                                                                                                                                                                                                                                                     // laserbeams[j].mesh.setTexture("bluebeam");
                                                                                                                                                                                                                                                                                     laserbeams[j].scale = 4.0f;
                                                                                                                                                                                                                                                                     }
                                                                                                                                                                                                                                                                     else
                                                                                                                                                                                                                                                                     {
                                                                                                                                                                                                                                                                                     // laserbeams[j].mesh.setTexture("greenbeam");
                                                                                                                                                                                                                                                                     }
                                                                                                                                                                                                                                                     }
                                                                                                                                                                                                                                                     if (ships[i].hardpoints[k].weapon.weapontype == 2) // standard
                                                                                                                                                                                                                                                                                                                                                                                                                                                                 // beam
                                                                                                                                                                                                                                                     {
                                                                                                                                                                                                                                                                     // laserbeams[j].beammesh.setTexture("purplebeam");
                                                                                                                                                                                                                                                     }

                                                                                                                                                                                                                                                     // laserbeams[j].beammesh.setTexture("purplebeam");

                                                                                                                                                                                                                                                     if (ships[i].team == 1)
                                                                                                                                                                                                                                                     {
                                                                                                                                                                                                                                                                     laserbeams[j].mesh.setTexture("redbeam");
                                                                                                                                                                                                                                                                     laserbeams[j].beammesh.setTexture("redbeam");
                                                                                                                                                                                                                                                     }
                                                                                                                                                                                                                                                     if (ships[i].team == 2)
                                                                                                                                                                                                                                                     {
                                                                                                                                                                                                                                                                     laserbeams[j].mesh.setTexture("bluebeam");
                                                                                                                                                                                                                                                                     laserbeams[j].beammesh.setTexture("bluebeam");

                                                                                                                                                                                                                                                     }
                                                                                                                                                                                                                                                     if (ships[i].team == 3)
                                                                                                                                                                                                                                                     {
                                                                                                                                                                                                                                                                     laserbeams[j].mesh.setTexture("yellowbeam");
                                                                                                                                                                                                                                                                     laserbeams[j].beammesh.setTexture("yellowbeam");

                                                                                                                                                                                                                                                     }
                                                                                                                                                                                                                                                     if (ships[i].team == 4)
                                                                                                                                                                                                                                                     {
                                                                                                                                                                                                                                                                     laserbeams[j].mesh.setTexture("greenbeam");
                                                                                                                                                                                                                                                                     laserbeams[j].beammesh.setTexture("greenbeam");

                                                                                                                                                                                                                                                     }
                                                                                                                                                                                                                                                     if (ships[i].team == 5)
                                                                                                                                                                                                                                                     {
                                                                                                                                                                                                                                                                     laserbeams[j].mesh.setTexture("purplebeam");
                                                                                                                                                                                                                                                                     laserbeams[j].beammesh.setTexture("purplebeam");

                                                                                                                                                                                                                                                     }

                                                                                                                                                                                                                                                     laserbeams[j].owninghardpointid = k;
                                                                                                                                                                                                                                                     /*
                                                                                                                                                                                                                                                         * if(ships[i]. hardpoints [k]. weapon.ismissile ) { //laserbeams [j ].mesh.setTexture ("redbeam"); } if(ships[i]. hardpoints [k].
                                                                                                                                                                                                                                                         * weapon.ismissile ==false && ships [i].hardpoints [ k].weapon.isbeam ==false)
{ // laserbeams [j]. mesh.setTexture ( "greenbeam"); }
                                                                                                                                                                                                                                                         */
                                                                                                                                                                                                                                                     laserbeams[j].mesh.setTransparency(255);
                                                                                                                                                                                                                                                     laserbeams[j].mesh.setTransparencyMode(Object3D.TRANSPARENCY_MODE_ADD);

                                                                                                                                                                                                                                                     laserbeams[j].beammesh.setTransparency(255);
                                                                                                                                                                                                                                                     laserbeams[j].beammesh.setTransparencyMode(Object3D.TRANSPARENCY_MODE_ADD);

                                                                                                                                                                                                                                                     // apply the
                                                                                                                                                                                                                                                     // relevant
                                                                                                                                                                                                                                                     // shader......
                                                                                                                                                                                                                                                     // in this case
                                                                                                                                                                                                                                                     // the basic
                                                                                                                                                                                                                                                     // shader will
                                                                                                                                                                                                                                                     // do.....
                                                                                                                                                                                                                                                     Log.d(TAG, "LaserBeam Shader Preparing");
                                                                                                                                                                                                                                                     laserbeams[j].mesh.setShader(this.shaders[1]);
                                                                                                                                                                                                                                                     laserbeams[j].beammesh.setShader(this.shaders[1]);
                                                                                                                                                                                                                                                     Log.d(TAG, "LaserBeam Shader Set");
                                                                                                                                                                                                                                                     laserbeams[j].mesh.build();
                                                                                                                                                                                                                                                     laserbeams[j].mesh.strip();

                                                                                                                                                                                                                                                     laserbeams[j].beammesh.build();
                                                                                                                                                                                                                                                     laserbeams[j].beammesh.strip();

                                                                                                                                                                                                                                                     world.addObject(laserbeams[j].mesh);
                                                                                                                                                                                                                                                     world.addObject(laserbeams[j].beammesh);
                                                                                                                                                                                                                                                     laserbeams[j].mesh.setOrigin(SimpleVector.ORIGIN);
                                                                                                                                                                                                                                                     laserbeams[j].beammesh.setOrigin(SimpleVector.ORIGIN);
                                                                                                                                                                                                                                                     laserbeams[j].movement = new SimpleVector(0, 0, 1);

                                                                                                                                                                                                                                                     pos.x = keyframe[currentframe].shipdata[i].fx[k];// x
                                                                                                                                                                                                                                                                                                                                                                                                                                                         // +
                                                                                                                                                                                                                                                                                                                                                                                                                                                         // pos.x;
                                                                                                                                                                                                                                                     pos.y = keyframe[currentframe].shipdata[i].fy[k];// y
                                                                                                                                                                                                                                                                                                                                                                                                                                                         // +
                                                                                                                                                                                                                                                                                                                                                                                                                                                         // pos.y;
                                                                                                                                                                                                                                                     pos.z = keyframe[currentframe].shipdata[i].fz[k];// z
                                                                                                                                                                                                                                                                                                                                                                                                                                                         // +
                                                                                                                                                                                                                                                                                                                                                                                                                                                         // pos.z;

                                                                                                                                                                                                                                                     laserbeams[j].mesh.translate(pos);
                                                                                                                                                                                                                                                     laserbeams[j].beammesh.translate(pos);

                                                                                                                                                                                                                                                     if (laserbeams[j].wmatrix == null)
                                                                                                                                                                                                                                                     {
                                                                                                                                                                                                                                                                     laserbeams[j].wmatrix = laserbeams[j].mesh.getWorldTransformation();
                                                                                                                                                                                                                                                     }
                                                                                                                                                                                                                                                     else
                                                                                                                                                                                                                                                     {
                                                                                                                                                                                                                                                                     laserbeams[j].wmatrix = laserbeams[j].mesh.getWorldTransformation(laserbeams[j].wmatrix);

                                                                                                                                                                                                                                                     }
                                                                                                                                                                                                                                                     if (laserbeams[j].wmatrixB == null)
                                                                                                                                                                                                                                                     {
                                                                                                                                                                                                                                                                     laserbeams[j].wmatrixB = laserbeams[j].beammesh.getWorldTransformation();
                                                                                                                                                                                                                                                     }
                                                                                                                                                                                                                                                     else
                                                                                                                                                                                                                                                     {
                                                                                                                                                                                                                                                                     laserbeams[j].wmatrixB = laserbeams[j].beammesh.getWorldTransformation(laserbeams[j].wmatrixB);
                                                                                                                                                                                                                                                     }
                                                                                                                                                                                                                                                     laserbeams[j].targetshipid = keyframe[currentframe].shipdata[i].firingframe[k] - 1;// -1;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 // //for
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 // now...
                                                                                                                                                                                                                                                     if (laserbeams[j].isbeam && laserbeams[j].targetshipid >= 0 && laserbeams[j].targetshipid < 32)
                                                                                                                                                                                                                                                     {
                                                                                                                                                                                                                                                                     laserbeams[j].movement.x = keyframe[currentframe].shipdata[laserbeams[j].targetshipid].x - keyframe[currentframe].shipdata[i].fx[k];
                                                                                                                                                                                                                                                                     laserbeams[j].movement.y = keyframe[currentframe].shipdata[laserbeams[j].targetshipid].y - keyframe[currentframe].shipdata[i].fy[k];
                                                                                                                                                                                                                                                                     laserbeams[j].movement.z = keyframe[currentframe].shipdata[laserbeams[j].targetshipid].z - keyframe[currentframe].shipdata[i].fz[k];

                                                                                                                                                                                                                                                                     // what we actually want to do.....is shift the beam from bottom to top through the enemy ship....
                                                                                                                                                                                                                                                                     // which will help the laser cutting when we split the mesh.....

                                                                                                                                                                                                                                                                     // perpendicular vector to laserbeam movement......

                                                                                                                                                                                                                                                                     // find a vector such that the dot product is 0....
                                                                                                                                                                                                                                                                     angle1 = 0f;
                                                                                                                                                                                                                                                                     if (ships[laserbeams[j].targetshipid].splitframe > 0)
                                                                                                                                                                                                                                                                     {
                                                                                                                                                                                                                                                                                     if (Math.abs(currentframe - ships[laserbeams[j].targetshipid].splitframe) < 25)
                                                                                                                                                                                                                                                                                     {
                                                                                                                                                                                                                                                                                                     angle1 = 6f * (float) (currentframe - ships[laserbeams[j].targetshipid].splitframe);
                                                                                                                                                                                                                                                                                     }

                                                                                                                                                                                                                                                                                     if (Math.abs(laserbeams[j].movement.x) > Math.abs(laserbeams[j].movement.y))
                                                                                                                                                                                                                                                                                     {
                                                                                                                                                                                                                                                                                                     // x is biggest so far
                                                                                                                                                                                                                                                                                                     if (Math.abs(laserbeams[j].movement.x) > Math.abs(laserbeams[j].movement.z))
                                                                                                                                                                                                                                                                                                     {
                                                                                                                                                                                                                                                                                                                     // x is definitely the biggest

                                                                                                                                                                                                                                                                                                                     if (Math.abs(laserbeams[j].movement.z) < 0.01f && Math.abs(laserbeams[j].movement.y) < 0.01f)
                                                                                                                                                                                                                                                                                                                     {
                                                                                                                                                                                                                                                                                                                                     // we are moving along x axis

                                                                                                                                                                                                                                                                                                                                     // shift movement y up/down by appropriate amount....

                                                                                                                                                                                                                                                                                                                                     laserbeams[j].movement.y += angle1;

                                                                                                                                                                                                                                                                                                                     }
                                                                                                                                                                                                                                                                                                                     else
                                                                                                                                                                                                                                                                                                                     {
                                                                                                                                                                                                                                                                                                                                     // find perpendicular....

                                                                                                                                                                                                                                                                                                                                     // dot = ix * jx + iy * jy + iz * jz = 0
                                                                                                                                                                                                                                                                                                                                     // set jx = 0 so...
                                                                                                                                                                                                                                                                                                                                     // iy*jy = - iz*jz
                                                                                                                                                                                                                                                                                                                                     //
                                                                                                                                                                                                                                                                                                                                     if (Math.abs(laserbeams[j].movement.z) < 0.01f)
                                                                                                                                                                                                                                                                                                                                     {
                                                                                                                                                                                                                                                                                                                                                     // perpendicular is going to be in the z direction....
                                                                                                                                                                                                                                                                                                                                                     laserbeams[j].movement.z += angle1;
                                                                                                                                                                                                                                                                                                                                     }
                                                                                                                                                                                                                                                                                                                                     else
                                                                                                                                                                                                                                                                                                                                     {
                                                                                                                                                                                                                                                                                                                                                     angle2 = (float) Math.sqrt(laserbeams[j].movement.x * laserbeams[j].movement.x + laserbeams[j].movement.y * laserbeams[j].movement.y + laserbeams[j].movement.z * laserbeams[j].movement.z);
                                                                                                                                                                                                                                                                                                                                                     if (angle2 > 0)
                                                                                                                                                                                                                                                                                                                                                     {
                                                                                                                                                                                                                                                                                                                                                                     laserbeams[j].movement.z += angle1 * (laserbeams[j].movement.y / angle2);
                                                                                                                                                                                                                                                                                                                                                                     laserbeams[j].movement.y += angle1 * (laserbeams[j].movement.z / angle2);
                                                                                                                                                                                                                                                                                                                                                     }

                                                                                                                                                                                                                                                                                                                                     }

                                                                                                                                                                                                                                                                                                                     }

                                                                                                                                                                                                                                                                                                     }
                                                                                                                                                                                                                                                                                                     else
                                                                                                                                                                                                                                                                                                     {
                                                                                                                                                                                                                                                                                                                     // z is the biggest
                                                                                                                                                                                                                                                                                                                     if (Math.abs(laserbeams[j].movement.x) < 0.01f && Math.abs(laserbeams[j].movement.y) < 0.01f)
                                                                                                                                                                                                                                                                                                                     {
                                                                                                                                                                                                                                                                                                                                     // we are moving along z axis
                                                                                                                                                                                                                                                                                                                                     laserbeams[j].movement.y += angle1;

                                                                                                                                                                                                                                                                                                                     }
                                                                                                                                                                                                                                                                                                                     else
                                                                                                                                                                                                                                                                                                                     {
                                                                                                                                                                                                                                                                                                                                     if (Math.abs(laserbeams[j].movement.y) < 0.01f)
                                                                                                                                                                                                                                                                                                                                     {
                                                                                                                                                                                                                                                                                                                                                     // perpendicular is going to be in the x direction....
                                                                                                                                                                                                                                                                                                                                                     laserbeams[j].movement.x += angle1;
                                                                                                                                                                                                                                                                                                                                     }
                                                                                                                                                                                                                                                                                                                                     else
                                                                                                                                                                                                                                                                                                                                     {
                                                                                                                                                                                                                                                                                                                                                     angle2 = (float) Math.sqrt(laserbeams[j].movement.x * laserbeams[j].movement.x + laserbeams[j].movement.y * laserbeams[j].movement.y + laserbeams[j].movement.z * laserbeams[j].movement.z);
                                                                                                                                                                                                                                                                                                                                                     if (angle2 > 0)
                                                                                                                                                                                                                                                                                                                                                     {
                                                                                                                                                                                                                                                                                                                                                                     laserbeams[j].movement.x += angle1 * (laserbeams[j].movement.y / angle2);
                                                                                                                                                                                                                                                                                                                                                                     laserbeams[j].movement.y += angle1 * (laserbeams[j].movement.z / angle2);
                                                                                                                                                                                                                                                                                                                                                     }

                                                                                                                                                                                                                                                                                                                                     }
                                                                                                                                                                                                                                                                                                                     }

                                                                                                                                                                                                                                                                                                     }
                                                                                                                                                                                                                                                                                     }
                                                                                                                                                                                                                                                                                     else
                                                                                                                                                                                                                                                                                     {
                                                                                                                                                                                                                                                                                                     // y is biggest so far
                                                                                                                                                                                                                                                                                                     if (Math.abs(laserbeams[j].movement.y) > Math.abs(laserbeams[j].movement.z))
                                                                                                                                                                                                                                                                                                     {
                                                                                                                                                                                                                                                                                                                     // y is definitely the biggest
                                                                                                                                                                                                                                                                                                                     if (Math.abs(laserbeams[j].movement.z) < 0.01f && Math.abs(laserbeams[j].movement.x) < 0.01f)
                                                                                                                                                                                                                                                                                                                     {
                                                                                                                                                                                                                                                                                                                                     // we are moving along y axis
                                                                                                                                                                                                                                                                                                                                     laserbeams[j].movement.x += angle1;

                                                                                                                                                                                                                                                                                                                     }
                                                                                                                                                                                                                                                                                                                     else
                                                                                                                                                                                                                                                                                                                     {
                                                                                                                                                                                                                                                                                                                                     if (Math.abs(laserbeams[j].movement.y) < 0.01f)
                                                                                                                                                                                                                                                                                                                                     {
                                                                                                                                                                                                                                                                                                                                                     // perpendicular is going to be in the y direction....
                                                                                                                                                                                                                                                                                                                                                     laserbeams[j].movement.y += angle1;
                                                                                                                                                                                                                                                                                                                                     }
                                                                                                                                                                                                                                                                                                                                     else
                                                                                                                                                                                                                                                                                                                                     {
                                                                                                                                                                                                                                                                                                                                                     angle2 = (float) Math.sqrt(laserbeams[j].movement.x * laserbeams[j].movement.x + laserbeams[j].movement.y * laserbeams[j].movement.y + laserbeams[j].movement.z * laserbeams[j].movement.z);
                                                                                                                                                                                                                                                                                                                                                     if (angle2 > 0)
                                                                                                                                                                                                                                                                                                                                                     {
                                                                                                                                                                                                                                                                                                                                                                     laserbeams[j].movement.x += angle1 * (laserbeams[j].movement.y / angle2);
                                                                                                                                                                                                                                                                                                                                                                     laserbeams[j].movement.y += angle1 * (laserbeams[j].movement.z / angle2);
                                                                                                                                                                                                                                                                                                                                                     }

                                                                                                                                                                                                                                                                                                                                     }

                                                                                                                                                                                                                                                                                                                     }

                                                                                                                                                                                                                                                                                                     }
                                                                                                                                                                                                                                                                                                     else
                                                                                                                                                                                                                                                                                                     {
                                                                                                                                                                                                                                                                                                                     // z is the biggest
                                                                                                                                                                                                                                                                                                                     if (Math.abs(laserbeams[j].movement.z) < 0.01f && Math.abs(laserbeams[j].movement.x) < 0.01f)
                                                                                                                                                                                                                                                                                                                     {
                                                                                                                                                                                                                                                                                                                                     // we are moving along z axis
                                                                                                                                                                                                                                                                                                                                     laserbeams[j].movement.x += angle1;

                                                                                                                                                                                                                                                                                                                     }
                                                                                                                                                                                                                                                                                                                     else
                                                                                                                                                                                                                                                                                                                     {
                                                                                                                                                                                                                                                                                                                                     if (Math.abs(laserbeams[j].movement.z) < 0.01f)
                                                                                                                                                                                                                                                                                                                                     {
                                                                                                                                                                                                                                                                                                                                                     // perpendicular is going to be in the z direction....
                                                                                                                                                                                                                                                                                                                                                     laserbeams[j].movement.z += angle1;
                                                                                                                                                                                                                                                                                                                                     }
                                                                                                                                                                                                                                                                                                                                     else
                                                                                                                                                                                                                                                                                                                                     {
                                                                                                                                                                                                                                                                                                                                                     angle2 = (float) Math.sqrt(laserbeams[j].movement.x * laserbeams[j].movement.x + laserbeams[j].movement.y * laserbeams[j].movement.y + laserbeams[j].movement.z * laserbeams[j].movement.z);
                                                                                                                                                                                                                                                                                                                                                     if (angle2 > 0)
                                                                                                                                                                                                                                                                                                                                                     {
                                                                                                                                                                                                                                                                                                                                                                     laserbeams[j].movement.x += angle1 * (laserbeams[j].movement.y / angle2);
                                                                                                                                                                                                                                                                                                                                                                     laserbeams[j].movement.z += angle1 * (laserbeams[j].movement.z / angle2);
                                                                                                                                                                                                                                                                                                                                                     }

                                                                                                                                                                                                                                                                                                                                     }

                                                                                                                                                                                                                                                                                                                     }
                                                                                                                                                                                                                                                                                                     }
                                                                                                                                                                                                                                                                                     }
                                                                                                                                                                                                                                                                     }
                                                                                                                                                                                                                                                     }
                                                                                                                                                                                                                                                     else
                                                                                                                                                                                                                                                     {
                                                                                                                                                                                                                                                                     laserbeams[j].movement.x = keyframe[currentframe].shipdata[i].fux[k] * laserbeams[j].scale;
                                                                                                                                                                                                                                                                     laserbeams[j].movement.y = keyframe[currentframe].shipdata[i].fuy[k] * laserbeams[j].scale;
                                                                                                                                                                                                                                                                     laserbeams[j].movement.z = keyframe[currentframe].shipdata[i].fuz[k] * laserbeams[j].scale;
                                                                                                                                                                                                                                                     }
                                                                                                                                                                                                                                                     if (laserbeams[j].rmatrix == null)
                                                                                                                                                                                                                                                     {
                                                                                                                                                                                                                                                                     laserbeams[j].rmatrix = laserbeams[j].mesh.getRotationMatrix();
                                                                                                                                                                                                                                                                     laserbeams[j].rmatrix = laserbeams[j].movement.getRotationMatrix(laserbeams[j].rmatrix);
                                                                                                                                                                                                                                                     }
                                                                                                                                                                                                                                                     else
                                                                                                                                                                                                                                                     {
                                                                                                                                                                                                                                                                     laserbeams[j].rmatrix = laserbeams[j].movement.getRotationMatrix(laserbeams[j].rmatrix);
                                                                                                                                                                                                                                                     }
                                                                                                                                                                                                                                                     if (laserbeams[j].rmatrixB == null)
                                                                                                                                                                                                                                                     {
                                                                                                                                                                                                                                                                     laserbeams[j].rmatrixB = laserbeams[j].beammesh.getRotationMatrix();
                                                                                                                                                                                                                                                                     laserbeams[j].rmatrixB = laserbeams[j].movement.getRotationMatrix(laserbeams[j].rmatrixB);
                                                                                                                                                                                                                                                     }
                                                                                                                                                                                                                                                     else
                                                                                                                                                                                                                                                     {
                                                                                                                                                                                                                                                                     laserbeams[j].rmatrixB = laserbeams[j].movement.getRotationMatrix(laserbeams[j].rmatrixB);
                                                                                                                                                                                                                                                     }
                                                                                                                                                                                                                                                     laserbeams[j].mesh.setRotationMatrix(laserbeams[j].rmatrix);
                                                                                                                                                                                                                                                     laserbeams[j].beammesh.setRotationMatrix(laserbeams[j].rmatrixB);
                                                                                                                                                                                                                                                     laserbeams[j].team = keyframe[currentframe].shipteam[i];

                                                                                                                                                                                                                                                     laserbeams[j].lifetime = ships[i].hardpoints[k].weapon.baselifetime;
                                                                                                                                                                                                                                                     laserbeams[j].guidedturnrate = ships[i].hardpoints[k].weapon.guidedturnrate;
                                                                                                                                                                                                                                                     laserbeams[j].isguided = ships[i].hardpoints[k].weapon.isguided;
                                                                                                                                                                                                                                                     laserbeams[j].isbeam = ships[i].hardpoints[k].weapon.isbeam;

                                                                                                                                                                                                                                                     if (laserbeams[j].isbeam)
                                                                                                                                                                                                                                                     {
                                                                                                                                                                                                                                                                     laserbeams[j].lifetime = 4; // beam will have a trail now...

                                                                                                                                                                                                                                                                     if (laserbeams[j].targetshipid >= 0 && laserbeams[j].targetshipid < 32)
                                                                                                                                                                                                                                                                     {
                                                                                                                                                                                                                                                                                     laserbeams[j].beammesh.setVisibility(true);
                                                                                                                                                                                                                                                                     }
                                                                                                                                                                                                                                                                     laserbeams[j].mesh.setVisibility(false);

                                                                                                                                                                                                                                                     }
                                                                                                                                                                                                                                                     else
                                                                                                                                                                                                                                                     {
                                                                                                                                                                                                                                                                     laserbeams[j].beammesh.setVisibility(false);
                                                                                                                                                                                                                                                                     laserbeams[j].mesh.setVisibility(true);
                                                                                                                                                                                                                                                     }
                                                                                                                                                                                                                                                     laserbeams[j].ismissile = ships[i].hardpoints[k].weapon.ismissile;
                                                                                                                                                                                                                                                     laserbeams[j].maxvel = ships[i].hardpoints[k].weapon.muzzlevel;
                                                                                                                                                                                                                                                     laserbeams[j].owningshipid = i;
                                                                                                                                                                                                                                                     laserbeams[j].owningshipteam = ships[i].team;
                                                                                                                                                                                                                                                     break;
                                                                                                                                                                                                                                     }
                                                                                                                                                                                                                                     // laserbeams[j].scale = 1f;
                                                                                                                                                                                                                                     if (laserbeams[j].lifetime <= 0)
                                                                                                                                                                                                                                     {
                                                                                                                                                                                                                                                     laserbeams[j].scale = 1.0f;
                                                                                                                                                                                                                                                     if (ships[i].hardpoints[k].weapon.weapontype == 4) // plasma
                                                                                                                                                                                                                                                     {
                                                                                                                                                                                                                                                                     // laserbeams[j].beammesh.setTexture("plasma");
                                                                                                                                                                                                                                                                     // laserbeams[j].mesh.setTexture("plasma");
                                                                                                                                                                                                                                         &