Script - Shield Management

From FreeSpace Wiki
Jump to: navigation, search

This script is designed to automate the shield management that is part of FS2's gameplay. It wil display a little element on your HUD underneath the reticle which will show you the current management status. This additional HUD gauge will look like four slices arranged in a circle. Each of these represents one of the four shield quadrants, with its colour changing depending on the script's status. White means the script is inactive, Blue means that a given quadrant is managed by the script, and red that the script is active, but not currently managing that quadrant.

In order to configure this script on-the-fly, the following keyboard controls are used:

  • q: unfocuses all quadrants (meaning the script is active, but not currently managing any shield areas
  • 0: Used to turn the script on and off
  • arrow keys: Used to turn a prticular quadrant on and off.

shieldman-sct.tbm

#Conditional Hooks

$Application: FS2_Open

$On Game Init: [[shields_onInit.LUA]]

$State: GS_STATE_GAME_PLAY
$On State Start: [[shields_onStart.LUA]]
$On Key Pressed: [[shields_KeyPressed.lua]]
$On Key Released: [[shields_KeyReleased.lua]]
$On Frame:[[shields_onFrame.lua]]
$On HUD Draw:[[shields_onHud.lua]]
$On Warp Out:[if hv.Self == hv.Player then IsWarpingOut = True end]

#End

Files to be placed in data/scripts

The following code snippets need to be copied into empty text files. These will need to be saved in data/scripts, with the names as indicated.

shields_KeyPressed.lua

local key = hv.Key
local func = ui_keyToggleFunctions[key]
if func then
	func(true)
end

shields_KeyReleased.lua

local key = hv.Key
local func = ui_keyToggleFunctions[key]
if func then
	func(false)
end

shields_OnFrame.lua

if not ShieldmanOverride then
	if MissionTime == nil then
	   MissionTime = mn.getMissionTime()
	   OldMissionTime = MissionTime
	end

	MissionTime = mn.getMissionTime()

	if OldMissionTime ~= MissionTime then
		--Keyboard responses
		if InputStates.zero then
			InputStates.zero = false
			if ShieldsEnabled then
				ShieldsEnabled = false
			else
				ShieldsEnabled = true
			end
		end
		if InputStates.qKey then
			InputStates.qKey = false
			FocusedShields[1] = false
			FocusedShields[2] = false
			FocusedShields[3] = false
			FocusedShields[4] = false
			NumberFocused = 0
		end
		if InputStates.right then
			InputStates.right = false
			if FocusedShields[1] then
				FocusedShields[1] = false
				NumberFocused = NumberFocused - 1
			else
				FocusedShields[1] = true
				NumberFocused = NumberFocused + 1
			end
		end
		if InputStates.up then
			InputStates.up = false
			if FocusedShields[2] then
				FocusedShields[2] = false
				NumberFocused = NumberFocused - 1
			else
				FocusedShields[2] = true
				NumberFocused = NumberFocused + 1
			end
		end
		if InputStates.down then
			InputStates.down = false
			if FocusedShields[3] then
				FocusedShields[3] = false
				NumberFocused = NumberFocused - 1
			else
				FocusedShields[3] = true
				NumberFocused = NumberFocused + 1
			end
		end
		if InputStates.left then
			InputStates.left = false
			if FocusedShields[4] then
				FocusedShields[4] = false
				NumberFocused = NumberFocused - 1
			else
				FocusedShields[4] = true
				NumberFocused = NumberFocused + 1
			end
		end

		gr.setColor(191,191,255,255)
		--Use all this only if it's turned on
		if ShieldsEnabled then

			plr = hv.Player
			if plr:isValid() then
				--If shields aren't full, then distribute the power
				if plr.Shields.CombinedMax > plr.Shields.CombinedLeft then
					qMax = ( plr.Shields.CombinedMax / 4 ) --Full energy for a quadrant
					Remainder = plr.Shields.CombinedLeft --Leftover shield energy
					--[1 to 3 Quads]
					if NumberFocused > 0 and NumberFocused < 4 then
						--check if there's insufficient energy to max out the focused quadrants
						if ( Remainder / NumberFocused ) < ( qMax ) then
							qMax = ( Remainder / NumberFocused )
							count = NumberFocused - 1
							Remainder = Remainder - ( count * qMax )
							for i = 1,4 do
								if FocusedShields[i] then
									if count > 1 then
										plr.Shields[i] = qMax
									else
										plr.Shields[i] = Remainder
									end
									count = count - 1
								end
							end
							for i = 1,4 do
								if FocusedShields[i] == false then
									plr.Shields[i] = 0
								end
							end
						else
							for i = 1,4 do
								if FocusedShields[i] then
									plr.Shields[i] = qMax
								end
							end
							Remainder = Remainder - ( qMax * NumberFocused )
							count = 4 - NumberFocused
							qDivided = ( Remainder / count )
							Remainder = Remainder - ( ( count - 1 ) * qDivided )
							for i = 1,4 do
								if FocusedShields[i] == false then
									if count > 1 then
										plr.Shields[i] = qDivided
									else
										plr.Shields[i] = Remainder
									end
									count = count - 1
								end
							end
						end
					else
					--[!1 to 3 quads]
						qDivided = ( Remainder / 4 )
						Remainder = Remainder - ( qDivided * 3 )
						for i = 1,3 do
							plr.Shields[i] = qDivided
						end
						plr.Shields[4] = Remainder
					end
					--[/1 to 3 quads]
				end
			end
		end
		OldMissionTime = MissionTime
	end
end

shields_onHud.lua

if not ShieldmanOverride then
	if hu.HUDDrawn == true and IsWarpingOut == false then
		drawShieldquadrants(FocusedShields, ShieldsEnabled)
	end
end

shields_onInit.lua

--Global vars
ShieldsEnabled = true

FocusedShields = { false, true, true, false }

NumberFocused = 2

ui_keyToggleFunctions = {}

quadrantenabledgfx = {}
quadrantenabledgfx[1] = gr.loadTexture("qrenabled")
quadrantenabledgfx[2] = gr.loadTexture("qfenabled")
quadrantenabledgfx[3] = gr.loadTexture("qaenabled")
quadrantenabledgfx[4] = gr.loadTexture("qlenabled")

quadrantdisabledgfx = {}
quadrantdisabledgfx[1] = gr.loadTexture("qrdisabled")
quadrantdisabledgfx[2] = gr.loadTexture("qfdisabled")
quadrantdisabledgfx[3] = gr.loadTexture("qadisabled")
quadrantdisabledgfx[4] = gr.loadTexture("qldisabled")

hudshieldbase = gr.loadTexture("shieldbase")

IsWarpingOut = false

shields = {}
shields.x = (gr.getScreenWidth() / 2) - (hudshieldbase:getWidth() / 2)
shields.y = (gr.getScreenHeight() / 3 * 2) - (hudshieldbase:getHeight() / 2)

drawShieldquadrants = function(quadrants, enabled)
    gr.drawImage(hudshieldbase, shields.x, shields.y)
	if enabled then
	    for i=1, 4 do
		   if quadrants[i] then gr.drawImage(quadrantenabledgfx[i], shields.x, shields.y)
		   else gr.drawImage(quadrantdisabledgfx[i], shields.x, shields.y)
		   end
		end
	end
end

shields_OnStart.lua

InputStates = {}
ui_keyToggleFunctions = {}
ui_keyToggleFunctions["Q"] = function(val) InputStates.qKey = val end
ui_keyToggleFunctions["0"] = function(val) InputStates.zero = val end
ui_keyToggleFunctions["Up Arrow"] = function(val) InputStates.up = val end
ui_keyToggleFunctions["Down Arrow"] = function(val) InputStates.down = val end
ui_keyToggleFunctions["Left Arrow"] = function(val) InputStates.left = val end
ui_keyToggleFunctions["Right Arrow"] = function(val) InputStates.right = val end
IsWarpingOut = false

Interface graphics

Download [this package], and unpack it into data/interface.

Credits

  • Script programming by ChronoReverse
  • Interface graphics by The E