Difference between revisions of "Script - Zoom"

From FreeSpace Wiki
Jump to: navigation, search
(Minor code cleanup)
(Now includes progress bar and control sensitivity)
Line 8: Line 8:
 
$On Mission Start:  
 
$On Mission Start:  
 
[
 
[
 +
-- Some things to tell the script to do something and some default values
 
runZoomScript = true
 
runZoomScript = true
 
zoomValue = 0.1
 
zoomValue = 0.1
 
normalFOV = 0.75
 
normalFOV = 0.75
 +
transitionTime = 2
 
zooming = false
 
zooming = false
 
defaultZoomSet = 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
 +
gr.drawString("Current: " .. currentProgr)
 +
gr.drawString("Frame: " .. thisFrameProg)
 +
gr.drawString("Zoom End: " .. zoomEndProgress)
 +
end
 
]
 
]
  
Line 18: Line 104:
 
$On Mission End:  
 
$On Mission End:  
 
[
 
[
runZoomScript = false
+
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
 
$State: GS_STATE_GAME_PLAY
$KeyPress: d
+
$On Key Pressed:  
$On Key Pressed: [
+
[
 
if runZoomScript then  
 
if runZoomScript then  
 +
if not cameraSet then
 +
if #gr.Cameras > 0 then
 +
cam = gr.Cameras[1]
 +
end
 +
end
 
if not zooming then
 
if not zooming then
 
plr = hv.Player
 
plr = hv.Player
 
if plr:isValid() then
 
if plr:isValid() then
if #gr.Cameras > 0 then
+
if cam:isValid() then
cam = gr.Cameras[1]
+
if cam:isValid() then
+
if currentProgr > 0 and currentProgr < 100 then
zooming = true
+
stepWidth = width / currentProgr -- Setting the stepWidth in case the zoom may not have been completed
if not defaultZoomSet then
 
normalFOV = cam.FOV
 
defaultZoomSet = true
 
end
 
zoom = normalFOV * zoomValue
 
cam:setFOV(zoom,2,1,0.5)
 
 
end
 
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
Line 45: Line 167:
 
]
 
]
  
$On Key Released: [
+
$KeyPress: d
 +
$State: GS_STATE_GAME_PLAY
 +
$On Key Released:
 +
[
 
if runZoomScript then  
 
if runZoomScript then  
 
if zooming then
 
if zooming then
 
plr = hv.Player
 
plr = hv.Player
 
if plr:isValid() then
 
if plr:isValid() then
if #gr.Cameras > 0 then
+
if cam:isValid() then
cam = gr.Cameras[1]
+
if cam:isValid() then
+
stepWidth = width / 100
zooming = false
+
cam:setFOV(normalFOV,2,1,0.5)
+
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
 
end
 +
 +
cam:setFOV(normalFOV,transitionTime,transitionTime / 2,transitionTime / 4)
 +
 +
ba.setControlMode(NORMAL_CONTROLS)
 
end
 
end
 
end
 
end

Revision as of 13:20, 17 July 2010

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
	gr.drawString("Current: " .. currentProgr)
	gr.drawString("Frame: " .. thisFrameProg)
	gr.drawString("Zoom End: " .. zoomEndProgress)
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