Global linktexture = LoadTexture("MOONS\triton.jpg")
;ScaleTexture linktexture,0.1,0.1

Function DeleteLinks()
	For L.Link = Each Link
		FreeEntity L\shape
		Delete L
	Next
End Function

Global wHole1 = LoadSprite("wHole1.tga", 3, cam)
Global wHole2 = LoadSprite("wHole2.jpg", 1, cam)
ScaleSprite wHole1, 2, 2
ScaleSprite wHole2, 3, 3
SpriteViewMode wHole1,2
SpriteViewMode wHole2,2
PositionEntity wHole1, 0,0,1
PositionEntity wHole2, 0,0,2

HideEntity wHole1
HideEntity wHole2


Global g_wormhole_maxticks = 5 * c_gameRate
Global g_wormhole_ticks = -1
Global g_wormhole_target.Planet

Function CreateLink(p1.Planet, p2.Planet)
	DebugLog "createlink: p1 = " + p1\name + " and p2 = " + p2\name
	If p1 = p2 Then Return
	;For L.Link = Each Link
	;	If (L\p1 = p1 And L\p2 = p2) Or (L\p1 = p2 And L\p2 = p1) Then Return	; link already exists
	;Next
	If p1 <> sol And p2 <> sol Then ;And p1 <> earth And p2 <> earth Then
		L.Link = New Link
		L\p1 = p1 : L\p2 = p2
		L\shape = CreateCube()
		EntityTexture L\shape,linktexture
		EntityFX L\shape,1	; full-bright

		PointEntity L\p2\campivot,L\p1\model
		
		g_wormhole_ticks = 0
		g_wormhole_target = L\p2
		archer\state = AT_LINK

		archer\timer = MilliSecs()	; timer resets when new link created
	EndIf
End Function

Function UpdateLinks()
	For L.Link = Each Link
		x# = EntityX(L\p1\model, True) : y# = EntityY(L\p1\model, True) : z# = EntityZ(L\p1\model, True)
		u# = EntityX(L\p2\model, True) : v# = EntityY(L\p2\model, True) : w# = EntityZ(L\p2\model, True)
		
		dist# = Abs(EntityDistance(L\p1\model, L\p2\model))
		vx# = (u - x) / dist : vy# = (v - y) / dist : vz# = (w - z) / dist	; compute unit vector
 
		;x1# = x + (vx * L\p1\radius) : y1# = y + (vy * L\p1\radius) : z1# = z + (vz * L\p1\radius) 
		
		If archer\state = AT_CORE Then
			If L\p1 = archer\loc Or L\p2 = archer\loc Then HideEntity L\shape
		Else
			ShowEntity L\shape
		EndIf
		
		PositionEntity L\shape,x,y,z,True
		PointEntity L\shape,L\p2\model
		MoveEntity L\shape,0,0,dist/2
		
		;dist = dist - L\p1\radius - L\p2\radius
		If archer\state = AT_SPACE
			radius# = 1.5
		Else
			radius# = 0.2
		EndIf
		ScaleEntity L\shape, radius, radius, dist/2
		
		;PositionEntity L\shape,x + (vx*dist/2),y + (vy*dist/2),z + (vz*dist/2),True

		;AlignToVector L\shape,vx,vy,vz,1
		;AlignToVector L\shape,vx,vy,vz,2
		;AlignToVector L\shape,vx,vy,vz,3
	Next
End Function

Function WormHole()
	If archer\state <> AT_LINK Or g_wormhole_ticks = -1 Then
		g_wormhole_ticks = -1
		HideEntity wHole1
		HideEntity wHole2
		Return
	ElseIf g_wormhole_ticks < c_gameRate/2 Then
		EntityParent cam,0
		HideEntity archer\bow
	ElseIf g_wormhole_ticks < c_gameRate Then
		g_wormhole_ticks = g_wormhole_ticks + 1
		SuperCam(cam, g_wormhole_target\model, 0.005, 0, 0, 0, 0)
	ElseIf Abs(EntityDistance(cam, g_wormhole_target\model)) < (g_wormhole_target\radius * 2) Then
		GoToOrbit(g_wormhole_target)
		g_wormhole_ticks = -1
		Return
	ElseIf Abs(EntityDistance(cam, g_wormhole_target\model)) < (g_wormhole_target\radius * 4) Then
		SuperCam(cam, g_wormhole_target\model, 0.01, 0, 0, 0, 0)
	ElseIf Abs(EntityDistance(cam, g_wormhole_target\model)) < (g_wormhole_target\radius * 3) Then
		SuperCam(cam, g_wormhole_target\model, 0.005, 0, 0, 0, 0)
	Else
		SuperCam(cam, g_wormhole_target\model, 0.1, 0, 0, 0, 0)
	EndIf

	If g_wormhole_target\sprite <> 0 Then HideEntity g_wormhole_target\sprite
	PointEntity cam,g_wormhole_target\model
	ShowPlanet(g_wormhole_target)
	
	g_wormhole_ticks = g_wormhole_ticks + 1
	If g_wormhole_ticks > g_wormhole_maxticks Then
		GoToOrbit(g_wormhole_target)
		g_wormhole_ticks = -1
		Return
	EndIf
	
	ShowEntity wHole1
	ShowEntity wHole2
	TurnEntity wHole1, 0,0,7
	TurnEntity wHole2, 0,0,-5
	
	percentage# = Float(wormhome_maxticks - g_wormhole_ticks) / wormhole_maxticks
End Function