var thisShip : GameObject;
static var hasPath : boolean=false;
var enemyTarget : GameObject;
var empty : GameObject;
function Update () {
if (hasPath==false){
var positions : Vector3[]=new Vector3[6*3*6*6*6*6*6];
var orientation : Vector3[]=new Vector3[6*3*6*6*6*6*6];
var enemyDamage : int[]=new int[3*6*6*6*6*6*6];
var damageTaken : int[]=new int[3*6*6*6*6*6*6];
var expectedGain : int[]=new int[3*6*6*6*6*6*6];
var counter : int=0;
for (var i : int=0; i<6; i++){
for (var e : int=0; e<6; e++){
for (var u : int=0; u<6; u++){
for (var n : int=0; n<3; n++){
for (var t : int=0; t<6; t++){
for (var m : int=0; m<6; m++){
for (var q : int=0; q<6; q++){
positions[counter]=findPosition(i*60, e*60, u*60, n*100);
orientation[counter]=Vector3(t*60, m*60, q*60);
enemyDamage[counter]=findDamage(enemyTarget, thisShip, positions[counter], orientation[counter], "attacker");
damageTaken[counter]=findDamage(thisShip, enemyTarget, positions[counter], orientation[counter], "defender");
expectedGain[counter]=calcGains(enemyDamage[counter], damageTaken[counter]);
counter++;
an=i;
}
}
}
}
}
}
}
var dec : int=1;
for (i=0; i<expectedGain.length; i++){
if (expectedGain>expectedGain[dec]){
dec=i;
}
}
hasPath=true;
}
if (hasPath==true){
GetComponent("ship guidance").positioning=positions[dec];
}
}
function findPosition(xangle : int, yangle : int, zangle : int, d : int){
var anobject : GameObject=Instantiate(empty, enemyTarget.transform.position, Quaternion.identity);
anobject.transform.rotation.eulerAngles.x=xangle;
anobject.transform.rotation.eulerAngles.y=yangle;
anobject.transform.rotation.eulerAngles.z=zangle;
anobject.transform.Translate(anobject.transform.forward*d);
var thepos : Vector3=anobject.transform.position;
Destroy(anobject);
return thepos;
}
// this code finds the damage that a ship will take from a resulting position
function findDamage(targetCraft : GameObject, attackingCraft : GameObject, positioning : Vector3, rotating : Vector3, dataType : String){
var totalDamage : int=0;
var initialObject : GameObject=Instantiate(empty, attackingCraft.transform.position, attackingCraft.transform.rotation);
if (dataType=="attacker"){
// there needs to be a way to move a 'ghosted' object around. How this will be done is a good question.
//this method should get it to work
initialObject.transform.position=positioning;
var totalWeps : Vector3[]=new Vector3[attackingCraft.GetComponent("attributes").weapons.length];
for (var i : int=0; i<attackingCraft.GetComponent("attributes").weapons.length; i++){
var dist : int=Vector3.Distance(attackingCraft.transform.position, attackingCraft.GetComponent("attributes").weaponsPosition);
var dir : Vector3=attackingCraft.transform.position-attackingCraft.GetComponent("attributes").weaoponsPosition;
dir=attackingCraft.transform.InverseTransformPoint(dir);
var originalRot : Vector3=initialObject.transform.rotation.eulerAngles;
initialObject.transform.rotation.eulerAngles=rotating;
dir=attackingCraft.transform.TransformPoint(dir);
var pos : Vector3=dist*dir;
totalWeps=pos;
}
for (i=0; i<attackingCraft.GetComponent("attributes").weapons.length; i++){
for (var n : int=0; n<targetCraft.GetComponent("attributes").partsListPosition.length; n++){
if (Physics.Linecast(totalWeps, targetCraft.GetComponent("attributes").partsListPosition[n])==false){
if (attackingCraft.GetComponent("attributes").weaponsDamage>=targetCraft.GetComponent("attributes").partsListHealth[n]+targetCraft.GetComponent("attributes").partsListArmor[n]){
totalDamage+=targetCraft.GetComponent("attributes").partsListExplosive[n];
}
if (attackingCraft.GetComponent("attributes").weaponsDamage<targetCraft.GetComponent("attributes").partsListHealth[n]+targetCraft.GetComponent("attributes").partsListArmor[n]){
totalDamage-=targetCraft.GetComponent("attributes").partsListArmor[n];
totalDamage+=attackingCraft.GetComponent("attributes").weaponsDamage;
}
}
}
}
Destroy(initialObject);
return totalDamage;
}
if (dataType=="defender"){
initialObject.transform.position=positioning;
initialObject.transform.rotation.eulerAngles=rotating;
for (i=0; i<targetCraft.GetComponent("attributes").weapons.length; i++){
for (n=0; n<(attackingCraft.GetComponent("attributes").bow/2)/10; n++){
if (Physics.Linecast(targetCraft.GetComponent("attributes").weaponsPosition, (initialObject.transform.position+(initialObject.transform.forward*n*10)))==false){
totalDamage+=targetCraft.GetComponent("attributes").weaponsDamage;
}
}
}
Destroy(initialObject);
return totalDamage;
}
}
function calcGains(damageDealed : int, damageTaken : int){
//let's toss in a calcRisk function some time from now
var totalGain : int=damageDealed-damageTaken;
return totalGain;
}
All those nested loops cycle through angles and distances for different positions and store data for each position/orientation set. The findPosition function simply finds the position from a distance from an orientation from the target ship. Then the calcDamage function finds how much damage is dealed to the enemy ship and how much damage is taken. The thing is that this function involves the use of instantiating objects. These objects should be deleted upon completion of the program, but instead, Unity keeps on drawing them, and the position data is not delivered to the ship guidance system. Anyone have any idea hat is causing this? I've even tried separating the loops into separate scripts, and even that yields the same result. Thanks for any assistance you can render (in advance)!