Hard Light Productions Forums

Modding, Mission Design, and Coding => FS2 Open Coding - The Source Code Project (SCP) => Topic started by: WMCoolmon on January 31, 2006, 01:27:18 am

Title: Scripting
Post by: WMCoolmon on January 31, 2006, 01:27:18 am
We don't have an official thread so I figured I'd start one.

The basics:
Function/class list (http://fs2source.warpcore.org/temp/scripting.html)

Hooks
All hooks have a +Override: attribute after them. If it's coded in for that particular hook, it controls whether original behavior is added to or overriden. "yes" "no", and various other equivelants in other languages are acceptable values.

Also, there are three types of hooks...

File - Use [[*.lua]]. This will run the contents of a file for the hook. (Note: the file is loaded and parsed when the hook is, so it's not really any different from using a normal hook.
Normal - Use [] with scripting inside the []. This will run the scripting.
Return - Here, the scripting must evaluate to a number and is constrained to a single line. Like, "ship.HitpointsLeft/ship.HitpointsMax". Generally this value will be used in some way. You can return values with a normal code block by using the "return" Lua command, if you want more than one line to work with.

Code: [Select]
#Global Hooks
$Global: ;;Executes every single time a frame is displayed
$Splash: ;;Executes once, when the splash screen is displayed (Override disables default splash). Override disables default splash.
$GameInit: ;;Executes after everything has been loaded, but the splash screen is still displayed. An ideal spot to put mn.loadMission(). Override does nothing.
$Simulation: ;;Executes after physics and such has been calculated. Should also be run before the player enters the mission.
$HUD: ;;Executes every frame that the HUD is displayed. +Override disables Game HUD
#End

#State Hooks
$State: ;;Can be any state. Look up GS_state_text in gamesequence.cpp for the full list. +Override disables normal FS2 state.
#End

gamesequence.cpp (http://fs2source.warpcore.org/cgi-bin/cvsweb/cvsweb.cgi/fs2_open/code/gamesequence/gamesequence.cpp?rev=2.9&content-type=text/x-cvsweb-markup) (Where you can find the list of states for state hooks)

SEXP
There's also a script-eval-num in CVS. This will take Normal and Return hook types. (You must use [] if the scripting doesn't return a number).

New stuff tonight

I've added some new weapons functions tonight, and it may not be clear on how they're used, because I haven't been able to get arrays working properly. So I'm using sub-objects for now. :doubt:

Anyway, something like this:

Code: [Select]
ship.PrimaryBanks[1].AmmoLeft
Should get you the number of rounds left for the first primary bank in a ship. Sub in SecondaryBanks for secondary weapons. It's totally untested ATM, but I figured that I should mention it before I forgot. ;)

EDIT: More info. It'd be nice if someone could add this stuff to the wiki...for some reason, I haven't been writing online very well.
Title: Re: Scripting
Post by: Vasudan Admiral on January 31, 2006, 10:06:52 pm
Well, it looks awesome and incredibly powerful, so big kudos for that, :) but I'm still rather confused about what it actually does.
I've had absolutely no experience in scripting, so forgive me if these are silly questions: ;)
Title: Re: Scripting
Post by: WMCoolmon on January 31, 2006, 11:00:48 pm
1) Yeah, sort of. What you'd do is you'd make a function in scripting, and then call it like:

script-eval-num "[function()]"

2) Yeah. You'd have to use campaign-persistent variables to save the $$ tho.

3) Not yet, I don't understand how the submodel animation code works...I tried adding it for an afterburner animation but didn't manage to get it to do anything.

4) You can do it via SEXPs with script-eval-num, at least for number values. I haven't implemented anything directly to let you set campaign variables yet, tho.

5) You should be able to add new things to the HUD with the HUD hook, or remove everything and start from scratch by specifying "+Override: YES" for the hud hook.

6) Hmm...you might be able to fake gravity with what's in now. Not very accurately, but you should be able to run through all the ships in the mission and set their position/velocity.

7) It can almost do render-to-texture for ships in D3D, it's just the texture replacement stuff that isn't working. You can also warp the same ship in as many times as you like. Grab information about previous missions from the campaign file. A lot of it needs to be tested before it can be verified 100%, but most of the functions are not too exotic.
Title: Re: Scripting
Post by: Vasudan Admiral on February 01, 2006, 05:41:08 pm
Awesome stuff. :)

1) Kewl. Are there plans to let it go the other way as well? Script > SEXP?

2) Does that mean the weapons effects viewer should be possible to script in? Ie, pretty much like your ships lab, but with weaponry. I've been hoping to eventually make something like that for ages, but I've just had the slight hinderance of not yet knowing how to code in C#/C++. ;)

3) Ok, fair enough. Might be something to keep in mind for future though, since script controlled animation would be incredibly useful for a huge number of things. :D

5) That's pretty awesome. So can we define things like graphic files to display and such?
Title: Re: Scripting
Post by: WMCoolmon on February 02, 2006, 03:15:21 am
1 & 2) Those sound useful enough to warrant a look, my coding comp is on the blink again though so no timeframe on that.

3) What I'd really like is the search function to find where Bobb posted how the animation system works. Unless it's in the internal (which has far fewer topics) it could take ages.

5) Yes, you just need to use gr.drawImage(name, x position, y position)
Title: Re: Scripting
Post by: Vasudan Admiral on February 02, 2006, 06:23:01 am
1 & 2) That'd be awesome, and good luck with your machine too. :\

3) Well.... I found this (http://www.hard-light.net/forums/index.php/topic,29238.0.html), this (http://www.hard-light.net/forums/index.php/topic,30930.0.html) and this (http://www.hard-light.net/forums/index.php/topic,30458.0.html) from an old post of Nuke's, but I get the feeling none of them are quite what you're after - they show how to use it rather than how it works.

5) Ok, and final related question from me for now: Can/will we be able to eventually create scripted stuff in the 3d engine while in-game? Stuff not just on the hud, but in 3d space - such as creating flat/shaped planes on which to draw special effects, 'virtual' lines that appear like the jump nodes as paths for the player to follow and stuff like that?
Title: Re: Scripting
Post by: Nuke on February 02, 2006, 01:01:25 pm
3)  i think he wants to see code on the internal, not the modder's side of things. the lack of a search feature is really really annoying. they should at least give it to coders.

5) will be cool. i plan to eventually do proper carrier landings complete with a proper ils gauge. not sure about scripted environments yet give them time. script functionality is progressing at an alarming rate. eventually wel get to the point where we ask for features and the coders will tell us to use the script :D
Title: Re: Scripting
Post by: Bobboau on February 09, 2006, 09:44:00 am
that's the idea
Title: Re: Scripting
Post by: WMCoolmon on February 09, 2006, 12:29:50 pm
My last commit to CVS removed all the "resize" variables from the 2D functions.
Title: Re: Scripting
Post by: WMCoolmon on February 15, 2006, 01:29:26 am
:bump:

Right before "$Impact Explosion:", I've added two new fields

I've also added "Weapon" and "Team" handles with some member variables/functions.

$Collide Ship:
Globals: "Self", weapon with class that the hook is defined in. "Ship", ship that the weapon collided with.
Override: Turns off all damage effects from weapon, as well as weapon being destroyed.

Executed whenever a weapon has hit a ship's hull.

$Collide Weapon:
Globals: "Self", weapon with class that the hook is defined in. "Weapon", other weapon.
Override: Turns off weapon killing types with bombs. If two weapons, weapon "A" (arbitrary) is preferred

This might be better as a global hook, but basically it's executed whenever two weapons collide. If you want to kill weapons like the code does here, set LifeLeft to 0.01 and DestroyedByWeapon to true.

Questions
Would the globals be better as "slf" "shp" "wpn", or should they be full names?



http://fs2source.warpcore.org/temp/wmc/C14022006.zip


Two words..."Freespace pong"
Title: Re: Scripting
Post by: FireCrack on February 15, 2006, 09:05:32 am
Hmm.. we could kind of use a function for applying damage...

Another function for spawning stuff would be cool too...
Title: Re: Scripting
Post by: Trivial Psychic on February 15, 2006, 09:16:53 am
$Collide Weapon:
Globals: "Self", weapon with class that the hook is defined in. "Weapon", other weapon.
Override: Turns off weapon killing types with bombs. If two weapons, weapon "A" (arbitrary) is preferred

This might be better as a global hook, but basically it's executed whenever two weapons collide. If you want to kill weapons like the code does here, set LifeLeft to 0.01 and DestroyedByWeapon to true.
Would this allow for interceptable primaries, such as the B5 defence grid?
Title: Re: Scripting
Post by: FireCrack on February 15, 2006, 01:01:26 pm
It should, only problem will be getting the AI to target primary laserbolts....
Title: Re: Scripting
Post by: WMCoolmon on February 15, 2006, 10:46:55 pm
Yup.

What do you mean by "applying damage" and "spawning stuff"? I can probably write createShip and createWeapon functions easily enough. For applying damage, you should be able to subtract damage from HitpointsLeft. Apparently I never coded the damage var in weapons.tbl, is that all you'd need?
Title: Re: Scripting
Post by: FireCrack on February 15, 2006, 11:03:20 pm
Well, it is possible to subtract damage from what's left, but doesnt that ten override a bunch of things, like the ship being jostled, the impat explosion, the flashing damage% in the target box, even the fancy new armour system.
Ofcourse, i realise that scripting is still in it's early stages.

By spawning stuff, i meant a generic "creat object" function would be optimal, but prehaps weapons have funky initilsation doo-dads that reqire special stuff. Either way.

Just me feeling a bit nitpicky when fiddling with the luascript stuff...
Title: Re: Scripting
Post by: Goober5000 on February 15, 2006, 11:24:21 pm
There's already create-ship and create-weapon sexps...
Title: Re: Scripting
Post by: WMCoolmon on February 26, 2006, 06:39:31 pm
:bump: I've made some updates to the drawImage functions, and the Afterburner variables. drawImage functions now take x2, y2 instead of width, height; the afterburner variables have been changed/added to AfterburnerFuelLeft and AfterburnerFuelMax for 'ship' and 'shipclass' handles.
Title: Re: Scripting
Post by: At on April 21, 2006, 10:52:19 pm
Hope this is a good place to ask. <.< >.>

I try to do this
Code: [Select]
DasShip = mn.getShipByIndex( Index )

-- checky stuff here

X, Y = DasShip.Position:getScreenCoords()

if X then
DrawTarget( X, Y )
end
Code: [Select]
function DrawTarget( X, Y )
gr.drawString( X, 5, 20 )
end
and FS dies.  Seems to die when ever I try to do something with X (or Y).  But it passes the "if X then" part. :x

If I replace the function's contents with gr.drawString( "p", 5, 20 ), it works fine.  And "p"s on my screen a million times because it's called a million times.

Here's what the whole thing looks like :
Code: [Select]
-- TestHud.lua
-- Test things here.

XMax = gr.getScreenWidth()
YMax = gr.getScreenHeight()
Index = 1

-- We do this so we don't have to call the function billions of times.
-- A variable is cheaper than a function call, performance wise.
NumShips = mn.getNumShips()

gr.setColor( 0, 255, 0, 96 )
gr.drawString( NumShips, 5, 5 )

-- Reticle
gr.drawLine( XMax / 2, YMax / 2 - 5, XMax / 2, YMax / 2 + 5 )
gr.drawLine( XMax / 2 - 5, YMax / 2, XMax / 2 + 5, YMax / 2 )



while Index < NumShips do
DasShip = mn.getShipByIndex( Index )

-- Is this ship alive? --
if DasShip then

if DasShip.HitpointsLeft > 0 then
-- Returning multiple values! :O
X, Y = DasShip.Position:getScreenCoords()


if X then
DrawTarget( X, Y )
end

end

end

Index = Index + 1
end
Code: [Select]
-- TestInit.lua
-- Initialize things for testing here.

-- Set these in the hud code.
XMax = 0
YMax = 0

function DrawTarget( X, Y )
gr.drawString( X, 5, 20 ) -- Always dies if X is used... Is X a number/int/real/whatever?  Or something far more sinister...?
end
Title: Re: Scripting
Post by: At on April 24, 2006, 06:59:31 pm
Hope I'm not being annoying by doing this, but earlier, I asked about a piece of script I was trying to get working (http://www.hard-light.net/forums/index.php/topic,38168.msg803485.html#msg803485), and didn't even get an "I don't know."  So, I thought I'd ask again.

I was using the 20060326 Mac OSX build.  I havn't tried this on my PC yet.

(Edit)
I tried this with the 20060421 Win32 build, and got the same result.  Well, not completely, I get a nice little errorbox telling what variable it failed on.  But it still dies in the same place.  Just thought it'd be good to mention I tried it here.
Title: Re: Scripting
Post by: WMCoolmon on April 25, 2006, 01:37:57 am
Erg, right. I did take a look at this but didn't see a good reason for it to just crash...but got derailed with something else bfore I could investigate it more fully.

Could you mantis this? :) Even just a link to the thread would be good...you don't need to repost the whole post, just a link would be fine. ;)
Title: Re: Scripting
Post by: At on April 25, 2006, 06:34:24 am
Done.   Wheeee, my first Mantis.