Author Topic: Scripts for the new build  (Read 2582 times)

0 Members and 1 Guest are viewing this topic.

Offline WMCoolmon

  • Purveyor of space crack
  • 213
Scripts for the new build
These were scripts made for questions people had, which make use of new features/fixes in my newest build.

getnumpressed-sct.tbm
Provides three functions for use in FRED: getNumPress, clearKeyPress, and showKeyPress

showKeyPress shows the last keypress and its default numeric value in white in the upper-left corner of the screen. Mostly for debugging.

getNumPress gets the numeric value of each keypress. It can read the number row, F1 row, or numpad keys. Default is to only read the number row. It always returns a value of 0-9 corresponding to the value on the key. If no key is in the buffer, it returns -1. For instance:
- when
-- =
--- script-eval-num "getNumPress(true,false,false)"
--- 8
-- send-training-msg "8 Pressed"

clearKeyPress clears the keypress buffer so that a previous keypress doesn't get logged by accident. Because FRED SEXPs don't execute in reaction to keypresses, the script stores the last keypress in a buffer until a new keypress is found.

Code: [Select]
#Conditional Hooks
$Application: Freespace 2
$On Game Init: [
--===clearKeyPress===--
--Clears the last key pressed, so you can be sure any calls to
--getNumPress are after this point
function clearKeyPress()
g_lastKeyPressed = nil
end

--===getNumPress===--
--Gets numeric value of a keypress, or -1 if not a numeric key
--ARGUMENTS
--includeNumrow: Includes ordinary number keys (default: YES)
--includeNumpad: Includes numpad keys (ignores numlock) (default: NO)
--includeFunction: Includes F* keys (default: NO)

function getNumPress(includeNumrow,includeNumpad,includeFunction)
if not g_lastKeyPressed then
return -1
end

--Default arguments
includeNumrow = includeNumrow or true
includeNumpad = includeNumpad or false
includeFunction = includeFunction or false

local numericKey = g_lastKeyPressed

--For "Pad *" keys
if includeNumpad and string.sub(numericKey,1,string.len("Pad "))=="Pad " then
numericKey = g_lastKeyPressed:sub(-1)
end
--For "F*" keys
if includeFunction and string.sub(numericKey,1,1)=="F" then
numericKey = g_lastKeyPressed:sub(-1)
end

--Return the value
if numericKey == "0" then return 0
elseif numericKey == "1" then return 1
elseif numericKey == "2" then return 2
elseif numericKey == "3" then return 3
elseif numericKey == "4" then return 4
elseif numericKey == "5" then return 5
elseif numericKey == "6" then return 6
elseif numericKey == "7" then return 7
elseif numericKey == "8" then return 8
elseif numericKey == "9" then return 9
else return -1
end
end

--===showKeyPress===--
--Shows key pressed and its default numeric value
--ARGUMENTS
--show: Whether to show key presses or not (default: YES)

function showKeyPress(show)
--Default arguments:
show = show or true
--Toggle
g_showKeyPressed = show
end
]
$On Frame: [
--===showKeyPress===--
if g_showKeyPressed and g_lastKeyPressed then
gr.setColor(255,255,255)
gr.drawString("KEY: '" .. g_lastKeyPressed .. "'")
gr.drawString("VALUE: '" .. getNumPress() .. "'")
end
--==================--
]
$On Key Released: [
--Save the value so other hooks can access it
g_lastKeyPressed = hv.Key
]
#End

shutdown-sct.tbm
Disables a ship's engines (but doesn't blow them up) when 'w' is pressed.

Code: [Select]
#Conditional Hooks
$Application: Freespace 2
$Keypress: w
$On Key Released: [
local player = mn.Ships['Alpha 1']

--If player is alive
if player:isValid() then
local engine = player['engine']

--And player has an engine subsystem
if engine:isValid() then

--If the keypress hasn't been used
if not g_PlayerEngineHitpoints then
--Save current engine hitpoints remaining
g_PlayerEngineHitpoints = engine.HitpointsLeft
--Set engine hitpoints to 0
engine.HitpointsLeft = 0
else
--Keypress has been used, restore player engine hitpoints
engine.HitpointsLeft = g_PlayerEngineHitpoints
--Reset stored hitpoints
g_PlayerEngineHitpoints = nil
end
end
end
]
#End

cockpit_crit-sct.tbm (Untested)
Destroys all of a ship's subsystems when the 'cockpit' subsystem is destroyed.
Code: [Select]
#Conditional Hooks
$Application: Freespace 2
$Object Type: Ship
$On Game Init: [
--===doCustomSubsysDamage===--
--Checks for any special effects that need to be
--applied to a ship due to subsystem strength
--ARGUMENTS
--ship Ship to check over and apply effects to
function doCustomSubsysDamage(ship)
if not ship:isValid() then
return false
end

local cockpit = ship['cockpit']

--In case of uppercase
if not cockpit:isValid() then
cockpit = ship['Cockpit']
end

if cockpit:isValid() then
if cockpit.HitpointsLeft < 1 then
local numSub = #ship
for i=0,numSub do
ship[i].HitpointsLeft = -1
end
end
end

return true
end
]
$On Asteroid Collision: [
doCustomSubsysDamage(hv.Self)
]
$On Debris Collision: [
doCustomSubsysDamage(hv.Self)
]
$On Ship Collision: [
doCustomSubsysDamage(hv.Self)
]
$On Weapon Collision: [
doCustomSubsysDamage(hv.Self)
]
#End
-C

 

Offline Topgun

  • 210
Re: Scripts for the new build

shutdown-sct.tbm
Disables a ship's engines (but doesn't blow them up) when 'w' is pressed.
awesome! can we use a keypress that uses ctrl or shift? like ctrl-s?

  

Offline WMCoolmon

  • Purveyor of space crack
  • 213
Re: Scripts for the new build
You'll be able to use Alt- and Shift- in my next build. hv.Key will be updated as well. It'll look like

"Alt-1"
"Alt-F12"
"Shift-1"

etc. :)
-C