FreeSpace Releases > Scripting Releases

Script: flicker glowmaps of dying ships

(1/4) > >>

zookeeper:
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: ---#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

--- End code ---

m!m:
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: ---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

--- End code ---
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.

Dragon:
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.

Spoon:

--- Quote from: Dragon on November 27, 2011, 06:42:41 am ---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.

--- End quote ---
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.
--- End quote ---

Dragon:
I was referring to this:

--- Quote ---Also, "debris-glow" is not flickered.
--- End quote ---
This is, as I understand, meant to prevent submodel debris from acting strangely. "debris" isn't the only map used in this manner.

Navigation

[0] Message Index

[#] Next page

Go to full version