Script - Zoom

From FreeSpace Wiki
Revision as of 13:28, 17 July 2010 by M!m (talk | contribs) (I forgot to remove the debug things)
Jump to: navigation, search

This script enables you to zoom your view so precise aiming is easier.

Table Entry

  • To use create an file called zoom-sct.tbm in your date/tables directory and paste the following code into it:
#Conditional Hooks

$Application: FS2_Open
$On Mission Start: 
[
-- Some things to tell the script to do something and some default values
runZoomScript = true
zoomValue = 0.1
normalFOV = 0.75
transitionTime = 2
zooming = false
defaultZoomSet = false
cameraSet = false

-- Setting the values to be used in the $On Frame: hook
currentProgr = 0
zoomEndProgress = 100

zoomingIn = false
zoomedIn = false

zoomingOut = false
zoomedOut = true

-- Constants for drawing the progress
offset_x = 30
offset_y = 70
heigth = 8
width = 80

stepWidth = width / 100

-- Settings for the sensitivity
sensitivity = 0.25

-- The camera which is used for zooming (is initialized in the first $Key Pressed: hook)
cam = nil

function getProgressString() 
	local progressString = "Zooming"
	if zoomingIn then
		progressString = progressString .. " in:"
	elseif zoomingOut then
		progressString = progressString .. " out:"
	elseif zoomedIn then
		progressString = "Zoomed in:"
	elseif zoomedOut then
		progressString = "Zoomed out:"
	else
		progressString = "Zoomed:"
	end
	return progressString
end

function handleControls()
	if zoomingIn or zoomedIn then
		local ci = ba.getControlInfo()
		ci.Pitch = ci.Pitch * sensitivity
		ci.Heading = ci.Heading * sensitivity
		ci.Bank = ci.Bank * sensitivity
	end
end

function drawProgress()
	progressString = getProgressString()
	
	local stringWidth = gr.getStringWidth(progressString);
	gr.drawString(progressString,30,70)
	local realOffset_x = offset_x + stringWidth + 10
	gr.drawRectangle(realOffset_x, offset_y,realOffset_x + width, offset_y + heigth, zoomedIn)
	local frameTime = ba.getFrametime()
	
	local progressLineOffset_x = realOffset_x + stepWidth * currentProgr
	gr.drawLine(progressLineOffset_x, offset_y, progressLineOffset_x, offset_y + heigth)
	
	local thisFrameProg = (frameTime / transitionTime) * zoomEndProgress
	if zoomingIn then
		currentProgr = currentProgr + thisFrameProg
		if currentProgr >= 100 then
			currentProgr = 100
			zoomingIn = false
			zoomedIn = true
		end
	elseif zoomingOut then
		currentProgr = currentProgr - thisFrameProg
		if currentProgr <= 0 then
			currentProgr = 0
			zoomedOut = true
			zoomingOut = false
			zooming = false
		end
	end
end
]

$Application: FS2_Open
$On Mission End: 
[
if runZoomScript then
	runZoomScript = false
	if defaultZoomSet then
		cam:setFOV(normalFOV) -- Resetting the FOV in case we have not zoomed out completely
	end
end
]

$Application: FS2_Open
$On Frame: 
[
if runZoomScript then	
	if zooming then
		handleControls()
	
		drawProgress()
	end
end
]

$KeyPress: d
$State: GS_STATE_GAME_PLAY
$On Key Pressed: 
[
if runZoomScript then 
	if not cameraSet then
		if #gr.Cameras > 0 then
			cam = gr.Cameras[1]
		end
	end
	if not zooming then
		plr = hv.Player
		if plr:isValid() then
			if cam:isValid() then
			
				if currentProgr > 0 and currentProgr < 100 then
					stepWidth = width / currentProgr -- Setting the stepWidth in case the zoom may not have been completed
				end
				
				zoomEndProgress = 100
			
				zooming = true
				
				zoomingIn = true
				zoomedIn = false
				zoomedOut = false
				zoomingOut = false
				
				if not defaultZoomSet then
					normalFOV = cam.FOV
					defaultZoomSet = true
				end
				zoom = normalFOV * zoomValue
				
				ba.setControlMode(LUA_FULL_CONTROLS)
				
				cam:setFOV(zoom,transitionTime,transitionTime / 2,transitionTime / 4)
			end
		end
	end
end
]

$KeyPress: d
$State: GS_STATE_GAME_PLAY
$On Key Released:
[
if runZoomScript then 
	if zooming then
		plr = hv.Player
		if plr:isValid() then
			if cam:isValid() then
			
				stepWidth = width / 100
			
				zoomingIn = false
				zoomingOut = true
				zoomedIn = false
				zoomedOut = false
				
				if currentProgr > 0 then
					zoomEndProgress = currentProgr -- Save the progress we made as we begin to zoom back
				end
				
				cam:setFOV(normalFOV,transitionTime,transitionTime / 2,transitionTime / 4)
				
				ba.setControlMode(NORMAL_CONTROLS)
			end
		end
	end
end
]

#End