// ********************************************************************************
// Code for working with lights.
// David Ikeda - 2004

// ********************************************************************************
[VarDef, includeLights, 0]
[If, #includeLights = 0,

// ********************************************************************************
[VarDef, cSunLight, 0]
[VarDef, cPointLight, 1]
[VarDef, cSpotLight, 2]
[VarDef, cGlowLight, 3]

[VarDef, cLightsID, 1556]
[VarDef, cLightTypeID, 1541]

[VarDef, cNumLights, 8]

[VarDef, lightStatus(#cNumLights) ]
[VarDef, lightPosition(3 * #cNumLights) ]

// ********************************************************************************
[RoutineDef, "GetLightStatus",
	[VarDef, i]
	[VarSet, i, 0]

	[VarDef, j]
	[VarSet, j, #cLightsID]
	
	[Loop, #cNumLights,
		[If, [IGet, #j], [VarSet, lightStatus(#i), 1]
			, [VarSet, lightStatus(#i), 0]
		]
		
		[VarInc, i]
		[VarInc, j]
	]
]

// ********************************************************************************
[RoutineDef, "SelectLight",
	[VarDef, lightIsOn][VarSet, lightIsOn, [IGet, [Val, #cLightsID + #index]] ]
	[If, #lightIsOn, [ISet, [Val, #cLightsID + #index], 0] ]
	[IPress, [Val, #cLightsID + #index] ]
	[ISet, [Val, #cLightsID + #index], #lightIsOn]
, index]

// ********************************************************************************
[RoutineDef, "GetLightPositions",
	[VarDef, j][VarSet, j, 0]
	[VarDef, offset][VarSet, offset, 0]

	[Loop, #cNumLights,
		[RoutineCall, "SelectLight", j]

		[VarSet, lightPosition(0 + #offset), [IGet, Light:Placement:X Pos] ]
		[VarSet, lightPosition(1 + #offset), [IGet, Light:Placement:Y Pos] ]
		[VarSet, lightPosition(2 + #offset), [IGet, Light:Placement:Z Pos] ]

		[VarAdd, offset, 3]
		[VarInc, j]
	]
]

// ********************************************************************************
[RoutineDef, "AdjustLights",
	[RoutineCall, "GetTransformation", #activeTransformation]
	[Note, #activeTransformation(6) ]
	[Note, #activeTransformation(7) ]
	[Note, #activeTransformation(8) ]
]

// ********************************************************************************
[RoutineDef, "GetLightType",
	[VarDef, id][VarSet, id, #cLightTypeID]
	[VarSet, lType, 0]

	[Loop, 4,
		[If, [IGet, #id], [LoopExit] ]
		[VarInc, lType]
		[VarInc, id]
	]
, lType]

// ********************************************************************************
[RoutineDef, "SetLightType",
	[ISet, [Val, #cLightTypeID + #ltype], 1]
, lType]

// ********************************************************************************
[RoutineDef, "GetXYZRotatedPosition",
	[RoutineCall, "CopyPosition", #sX, #sY, #sZ, #dx, #dY, #dZ]
	[RoutineCall, "RotateX", #dx, #dY, #dZ, #angleX]
	[RoutineCall, "RotateY", #dx, #dY, #dZ, #angleY]
	[RoutineCall, "RotateZ", #dx, #dY, #dZ, #angleZ]
, sX, sY, sZ, dX, dY, dZ, angleX, angleY, angleZ]

// ********************************************************************************
[RoutineDef, "SetLightPosition",
	/* Temporarily set to point light to adjust position. This is to address a
	 * strange issue I found with ZBrush with Sun lights where changing the positional
	 * values had no effect on the position of the Sun until I switched the type
	 * to point and back to sun. */
	[VarDef, temp]
	[RoutineCall, "GetLightType", #temp]
	[RoutineCall, "SetLightType", #cPointLight]
	
	[ISet, Light:Placement:X Pos, #x]
	[ISet, Light:Placement:Y Pos, #y]
	[ISet, Light:Placement:Z Pos, #z]
	
	[RoutineCall, "SetLightType", #temp]
, x, y, z]

// ********************************************************************************
[RoutineDef, "RotateLights", 
	[VarDef, j][VarSet, j, 0]
	[VarDef, offset][VarSet, offset, 0]

	[Loop, #cNumLights,
		[If, #lightStatus(#j), 
			[RoutineCall, "SelectLight", #j]
	
			[VarDef, tempX]
			[VarDef, tempY]
			[VarDef, tempZ]
	
			[RoutineCall, "GetXYZRotatedPosition", 
				#lightPosition(0 + #offset), 
				#lightPosition(1 + #offset), 
				#lightPosition(2 + #offset), 
				#tempX, #tempY, #tempZ, 
				#angleX, #angleY, #angleZ
			]
	
			[RoutineCall, "SetLightPosition", #tempX, #tempY, #tempZ]
		]
		[VarAdd, offset, 3]
		[VarInc, j]
	]
	
, angleX, angleY, angleZ]

// ********************************************************************************
][VarSet, includeLights, 1]
