Author Topic: Script: flicker glowmaps of dying ships  (Read 5328 times)

0 Members and 2 Guests are viewing this topic.

Offline zookeeper

  • *knock knock* Who's there? Poe. Poe who?
  • 210
Script: flicker glowmaps of dying ships
It bothered me how glowmaps of dying capships stay on, so I wrote a little script which makes them flicker and gradually turn off altogether over the course of a few seconds. As you can see, the flicker duration is changeable by changing the value of flicker_duration. Also, "debris-glow" is not flickered.

Unfortunately, it's impossible to access the textures of debris pieces, so this is incapable of switching debris piece glowmaps off.

Feel free to use this however and wherever you wish. If you find any bugs or make improvements, post them here!

Code: [Select]
#Conditional Hooks

$Application: FS2_Open

$On Game Init: [

    -- For how long the glowmaps should flicker, in seconds
    flicker_duration = 4.0

    glowmap_replacement = gr.createTexture(16, 16, TEXTURE_DYNAMIC)

    if glowmap_replacement == nil or not glowmap_replacement:isValid() then
        ba.warning("Error in initializing glowmap flickering script: could not create helper texture! Flicker effect won't work.")
    else
        gr.setTarget(glowmap_replacement)
        gr.clearScreen()
        gr.setTarget()
    end

]

$State: GS_STATE_GAME_PLAY

$On State Start: [

    flickering_glowmaps = {}

]

$On State End: [

    flickering_glowmaps = {}

]

$On Death: [

    ship = hv.Self

    if ship ~= nil and ship:isValid() and glowmap_replacement ~= nil and glowmap_replacement:isValid() then
        textures_num = #ship.Textures
        for i=1,textures_num do
            filename = ship.Textures[i]:getFilename()

            if string.find(filename, "-glow") ~= nil and string.find(filename, "debris-glow") == nil then
                table.insert(flickering_glowmaps, { ship_signature = ship:getSignature(), tex_i = i, tex = ship.Textures[i], start_time = mn.getMissionTime() } )
            end
        end
    end

]

$On Frame: [

    if table.maxn(flickering_glowmaps) > 0 then
        for i=1,table.maxn(flickering_glowmaps) do
            flicker_time_elapsed = mn.getMissionTime() - flickering_glowmaps[i].start_time

            if flicker_time_elapsed < flicker_duration then
                ship = mn.getObjectFromSignature(flickering_glowmaps[i].ship_signature)

                if (math.random() * flicker_duration) > flicker_time_elapsed then
                    ship.Textures[flickering_glowmaps[i].tex_i] = flickering_glowmaps[i].tex
                else
                    ship.Textures[flickering_glowmaps[i].tex_i] = glowmap_replacement
                end
            else
                table.remove(flickering_glowmaps, i)

                -- Removing an element might make the loop break, so let's just
                -- bail out for this frame rather than do something fancy...
                break;
            end
        end
    end

]

#End

 

Offline m!m

  • 211
Re: Script: flicker glowmaps of dying ships
Nice script, I've been working on something similar :yes:
Here are a few things I noticed:

Is there a reason why you don't use the "deactivate-glow-maps" SEXP? That might be faster than doing texture replacement.

Your "On State End" may be executed although the game is still in the gameplay state (for example when you pause the game). You could use the "On Mission Start/End" hooks here to avoid unintended behavior.

You can iterate over a table and remove elements from it using a while-for loop construct like the following:
Code: [Select]
local index = 0
local done = false

while not done do
    for i = index, #table do
        table.remove(table, i)
        index = i
        break
    end

    done = true
end
If you want to process the whole table in one frame. That way you won't have some dead entries in it.

I also encountered a problem while developing my script with time compression which generated some non realistic looking results as the glowmaps were turned on and off every frame so I added a delay of 0.25 seconds so the script only changes the state four times per seconds independent from the actual frametime.

 

Offline Dragon

  • Citation needed
  • 212
  • The sky is the limit.
Re: Script: flicker glowmaps of dying ships
Looks great, but it won't stop all debris glows from flickering.
For instance, a fair number of ships use "damage" and "DocTile6" (or "DocTile6a") textures for a debris map. I think that an easily editable config for excluded maps (and flicker time, maybe also per-ship if possible) would be a good idea.

 

Offline Spoon

  • 212
  • ヾ(´︶`♡)ノ
Re: Script: flicker glowmaps of dying ships
Looks great, but it won't stop all debris glows from flickering.
For instance, a fair number of ships use "damage" and "DocTile6" (or "DocTile6a") textures for a debris map. I think that an easily editable config for excluded maps (and flicker time, maybe also per-ship if possible) would be a good idea.
hurr durr, reading is hard.

Quote from: zookeeper
Unfortunately, it's impossible to access the textures of debris pieces, so this is incapable of switching debris piece glowmaps off.
Urutorahappī!!

[02:42] <@Axem> spoon somethings wrong
[02:42] <@Axem> critically wrong
[02:42] <@Axem> im happy with these missions now
[02:44] <@Axem> well
[02:44] <@Axem> with 2 of them

 

Offline Dragon

  • Citation needed
  • 212
  • The sky is the limit.
Re: Script: flicker glowmaps of dying ships
I was referring to this:
Quote
Also, "debris-glow" is not flickered.
This is, as I understand, meant to prevent submodel debris from acting strangely. "debris" isn't the only map used in this manner.

 

Offline zookeeper

  • *knock knock* Who's there? Poe. Poe who?
  • 210
Re: Script: flicker glowmaps of dying ships
Nice script, I've been working on something similar :yes:
Here are a few things I noticed:

Is there a reason why you don't use the "deactivate-glow-maps" SEXP? That might be faster than doing texture replacement.

Well, yes, that deactivates all glow maps, whereas I want to exclude debris-glow.

Your "On State End" may be executed although the game is still in the gameplay state (for example when you pause the game). You could use the "On Mission Start/End" hooks here to avoid unintended behavior.

I guess you're right about pausing. However, the "On Mission Start/End" hooks are at least as problematic, because they don't get triggered when you quick-restart the mission after you die. Stuff like this is notoriously difficult to deal with as far as I've discovered, due to that unfortunate feature.

Basically, as silly as it sounds, there's no hook/condition combination which would trigger when the gameplay part of a mission starts. Unless I'm missing something.

 

Offline m!m

  • 211
Re: Script: flicker glowmaps of dying ships
Well, yes, that deactivates all glow maps, whereas I want to exclude debris-glow.
Hmm, why don't you want to disable debris-glow?

I guess you're right about pausing. However, the "On Mission Start/End" hooks are at least as problematic, because they don't get triggered when you quick-restart the mission after you die. Stuff like this is notoriously difficult to deal with as far as I've discovered, due to that unfortunate feature.

Basically, as silly as it sounds, there's no hook/condition combination which would trigger when the gameplay part of a mission starts. Unless I'm missing something.
The last times when I used that hook it worked as expected even when doing a quick start. You can do initialization when the gameplay starts by doing it in the first execution of the "On Frame"-hook. You will have to check for "mn.getMissionTime() ~= 0" or you will get some weird results when doing a quick start as some things are not initialized correctly then.

  

Offline zookeeper

  • *knock knock* Who's there? Poe. Poe who?
  • 210
Re: Script: flicker glowmaps of dying ships
Well, yes, that deactivates all glow maps, whereas I want to exclude debris-glow.
Hmm, why don't you want to disable debris-glow?

Because "debris" is the damage texture I use on -destroyed models, and its glowmap shouldn't flicker, because it isn't used to represent lights, windows, engine glows etc that the other glowmaps are used for.

I guess you're right about pausing. However, the "On Mission Start/End" hooks are at least as problematic, because they don't get triggered when you quick-restart the mission after you die. Stuff like this is notoriously difficult to deal with as far as I've discovered, due to that unfortunate feature.

Basically, as silly as it sounds, there's no hook/condition combination which would trigger when the gameplay part of a mission starts. Unless I'm missing something.
The last times when I used that hook it worked as expected even when doing a quick start. You can do initialization when the gameplay starts by doing it in the first execution of the "On Frame"-hook. You will have to check for "mn.getMissionTime() ~= 0" or you will get some weird results when doing a quick start as some things are not initialized correctly then.

I tested, and no, $On Mission Start hooks do not run when doing a quick restart (the one you can pick when you die, not the restart option from the pause menu). When you start a mission, that hook runs before the briefing comes up so I don't think it's ever been triggered by a quick restart.

Actually I think we should add a $On Mission Gameplay Start hook which would run whenever the gameplay of a mission starts. Would make stuff like this a whole lot easier to do.

 

Offline Alan Bolte

  • 28
  • Deneb III
    • @Compellor
Re: Script: flicker glowmaps of dying ships
Did using "mn.getMissionTime() ~= 0" instead of $On Mission Start and ending the script with $On Mission End not work for you then zookeeper?
Anything worth doing is worth analyzing to death -Iranon

 

Offline Nighteyes

  • 211
Re: Script: flicker glowmaps of dying ships
M!M might this make you finish the death roll script?? :P

its a shame people waste time working on the same thing...

 

Offline m!m

  • 211
Re: Script: flicker glowmaps of dying ships
:sigh: Fine, I think I fixed the last issue so I'll release it later today if works...
EDIT: Or not, the required functions aren't in trunk yet...
Also sorry for off-topic.
« Last Edit: November 28, 2011, 06:00:34 am by m!m »

 

Offline zookeeper

  • *knock knock* Who's there? Poe. Poe who?
  • 210
Re: Script: flicker glowmaps of dying ships
Did using "mn.getMissionTime() ~= 0" instead of $On Mission Start and ending the script with $On Mission End not work for you then zookeeper?

You mean something like this?

Code: [Select]
$State: GS_STATE_GAME_PLAY
$On State Start: [

    if mn.getMissionTime() < 1.0 then

Yeah, that'd probably work. Should go without saying though that it'd be better to have direct code support for such a hook.

 

Offline m!m

  • 211
Re: Script: flicker glowmaps of dying ships
You will have to add a "mn.getMissionTime() ~= 0" condition or the hook will be executed in the loading screen when you do a quick start. I already had this problem and it took me ages to find that out...
But a special hook is definitely the best solution here.

 

Offline Dragon

  • Citation needed
  • 212
  • The sky is the limit.
Re: Script: flicker glowmaps of dying ships
:sigh: Fine, I think I fixed the last issue so I'll release it later today if works...
EDIT: Or not, the required functions aren't in trunk yet...
Also sorry for off-topic.
Can't you provide a custom build? That'll most likely get coders' attention and it should quickly get commited.

 

Offline mjn.mixael

  • Cutscene Master
  • 212
  • Chopped liver
    • Steam
    • Twitter
Re: Script: flicker glowmaps of dying ships
Resurrecting this to ask it'd be possible to get it to affect glowpoints too.
Cutscene Upgrade Project - Mainhall Remakes - Between the Ashes
Youtube Channel - P3D Model Box
Between the Ashes is looking for committed testers, PM me for details.
Freespace Upgrade Project See what's happening.

 

Offline zookeeper

  • *knock knock* Who's there? Poe. Poe who?
  • 210
Re: Script: flicker glowmaps of dying ships
Resurrecting this to ask it'd be possible to get it to affect glowpoints too.

Well, sure, that's easy...

Code: [Select]
#Conditional Hooks

$Application: FS2_Open

$On Game Init: [

    -- For how long the glowmaps should flicker, in seconds
    flicker_duration = 4.0

    glowmap_replacement = gr.createTexture(16, 16, TEXTURE_DYNAMIC)

    if glowmap_replacement == nil or not glowmap_replacement:isValid() then
        ba.warning("Error in initializing glowmap flickering script: could not create helper texture! Flicker effect won't work.")
    else
        gr.setTarget(glowmap_replacement)
        gr.clearScreen()
        gr.setTarget()
    end

]

$State: GS_STATE_GAME_PLAY

$On State Start: [

    flickering_glowmaps = {}

]

$On State End: [

    flickering_glowmaps = {}

]

$On Death: [

    ship = hv.Self

    if ship ~= nil and ship:isValid() and glowmap_replacement ~= nil and glowmap_replacement:isValid() then
        textures_num = #ship.Textures
        for i=1,textures_num do
            filename = ship.Textures[i]:getFilename()

            if string.find(filename, "-glow") ~= nil and string.find(filename, "debris-glow") == nil then
                table.insert(flickering_glowmaps, { ship_signature = ship:getSignature(), tex_i = i, tex = ship.Textures[i], start_time = mn.getMissionTime() } )
            end
        end
    end

]

$On Frame: [

    if table.maxn(flickering_glowmaps) > 0 then
        for i=1,table.maxn(flickering_glowmaps) do
            flicker_time_elapsed = mn.getMissionTime() - flickering_glowmaps[i].start_time

            if flicker_time_elapsed < flicker_duration then
                ship = mn.getObjectFromSignature(flickering_glowmaps[i].ship_signature)

                if (math.random() * flicker_duration) > flicker_time_elapsed then
                    ship.Textures[flickering_glowmaps[i].tex_i] = flickering_glowmaps[i].tex

                    mn.runSEXP("activate-glow-points !" .. ship.Name .. "!")
                else
                    ship.Textures[flickering_glowmaps[i].tex_i] = glowmap_replacement

                    mn.runSEXP("deactivate-glow-points !" .. ship.Name .. "!")
                end
            else
                table.remove(flickering_glowmaps, i)

                -- Removing an element might make the loop break, so let's just
                -- bail out for this frame rather than do something fancy...
                break;
            end
        end
    end

]

#End

Only added the two mn.runSEXP lines. Untested, though.