/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package org.xtx.ut4converter.t3d;

import javax.vecmath.Vector3d;
import org.xtx.ut4converter.MapConverter;
import org.xtx.ut4converter.UTGames;
import org.xtx.ut4converter.tools.Geometry;

/**
 * Base class for teleporters
 * 
 * @author XtremeXp
 */
public class T3DTeleporter extends T3DSound {

	/**
	 * Unreal 1 property refering to
	 */
	String url;

	public T3DTeleporter(MapConverter mc, String t3dClass) {
		super(mc, t3dClass);
	}

	@Override
	public boolean analyseT3DData(String line) {

		// UE1 -> "URL="hepburn2"
		if (line.contains("URL=")) {
			url = line.split("\\=")[1];
		} else {
			return super.analyseT3DData(line);
		}

		return true;
	}

	@Override
	public String toString() {

		// only write if we have data about linked teleporter
		if (mapConverter.getOutputGame() == UTGames.UTGame.UT4 && linkedTo != null && !linkedTo.isEmpty()) {

			sbf.append(IDT).append("Begin Actor Class=BP_Teleporter_New_C Name=").append(name).append("\n");
			sbf.append(IDT).append("\tBegin Object Name=\"TriggerBox\"\n");

			// to reset rotation of teleporter else teleporter destination
			// invalid is rotation set
			// TODO maybe make rotation the translation vector instead of doing
			// this
			this.rotation = null;
			writeLocRotAndScale();
			sbf.append(IDT).append("\tEnd Object\n");
			sbf.append(IDT).append("\tTriggerBox=TriggerBox\n");

			// Note UT4 only support teleporting to one possible location
			// unlike U1/UT99/? do support multiple destinations
			T3DTeleporter linkedTel = (T3DTeleporter) linkedTo.get(0);
			Vector3d t = Geometry.sub(linkedTel.location, this.location);
			linkedTel.rotation = null;

			sbf.append(IDT).append("\tTeleportTarget=(Translation=(X=").append(t.x).append(",Y=").append(t.y).append(",Z=").append(t.z).append("))\n");
			sbf.append(IDT).append("\tRootComponent=TriggerBox\n");
			writeEndActor();

			linkedTo.clear(); // needs to remove linked teleporter or else loop
								// on writting linked teleporter
			return sbf.toString() + linkedTel.toString();
		} else {
			return "";
		}
	}

	@Override
	public void convert() {

		// we need to retrieve the linked teleporters
		// at this stage the T3D level converter
		// may not have yet parsed data of destination teleporter
		if (linkedTo == null || linkedTo.isEmpty()) {
			T3DLevelConvertor tlc = mapConverter.getT3dLvlConvertor();

			for (T3DActor actor : tlc.convertedActors) {
				if (actor instanceof T3DTeleporter) {

					if (actor.tag != null && this.url != null && this.url.equals("\"" + actor.tag + "\"")) {
						this.linkedTo.add(actor);
						actor.linkedTo.add(this);
						break;
					}
				}
			}
		}

		super.convert();
	}
}

@@ -1042,51 +1042,8 @@
 .UT4
- && linkedTo != null && !linkedTo.isEmpty()
 ) %7B%0D
@@ -1552,24 +1552,76 @@
 Box%5Cn%22);%0D%0A%0D%0A
+%09%09%09if (linkedTo != null && !linkedTo.isEmpty()) %7B%0D%0A%09
 %09%09%09// Note U
@@ -1677,16 +1677,17 @@
 ion%0D%0A%09%09%09
+%09
 // unlik
@@ -1732,16 +1732,17 @@
 ations%0D%0A
+%09
 %09%09%09T3DTe
@@ -1799,16 +1799,17 @@
 0);%0D%0A%09%09%09
+%09
 Vector3d
@@ -1862,24 +1862,25 @@
 ation);%0D%0A%09%09%09
+%09
 linkedTel.ro
@@ -1893,24 +1893,25 @@
  = null;%0D%0A%0D%0A
+%09
 %09%09%09sbf.appen
@@ -2038,24 +2038,27 @@
 d(%22))%5Cn%22);%0D%0A
+%0D%0A%09
 %09%09%09sbf.appen
@@ -2104,24 +2104,25 @@
 ox%5Cn%22);%0D%0A%09%09%09
+%09
 writeEndActo
@@ -2129,16 +2129,17 @@
 r();%0D%0A%0D%0A
+%09
 %09%09%09linke
@@ -2206,20 +2206,16 @@
 op%0D%0A%09%09%09%09
-%09
-%09%09%09
 // on wr
@@ -2240,16 +2240,17 @@
 porter%0D%0A
+%09
 %09%09%09retur
@@ -2291,16 +2291,146 @@
 ing();%0D%0A
+%09%09%09%7D else %7B%0D%0A%09%09%09%09sbf.append(IDT).append(%22%5CtRootComponent=TriggerBox%5Cn%22);%0D%0A%09%09%09%09writeEndActor();%0D%0A%09%09%09%09return sbf.toString();%0D%0A%09%09%09%7D%0D%0A
 %09%09%7D else

write teleporter even if no linked teleporter found