Author Topic: Zooming by run loop script  (Read 125 times)

0 Members and 1 Guest are viewing this topic.

bitterman

  • Sr. Member
  • ****
  • Posts: 270
  • Karma: +8/-5
  • Doom Newbie
    • View Profile
Zooming by run loop script
« on: September 02, 2017, 01:09:06 PM »
I trying to achieve a sniper zooming effect via 'g_fov' CVar.

It's realized via level script but as I think same algorithm can be used in weapon script.

The example below is works but from time to time g_fov is not set correct (stays in previous state or sets several times in one pass).

I thing it's because cycle checks is too fast.

Has anyone idea how to fix it?

Thanks.

Code: [Select]
void    debug_print()
        {
        sys.println( "^3g_fov set to: " + sys.getcvar( "g_fov" ) + " ^5at:" + sys.getTime() + "sec" );
        }

void    pressed_fov()
        {
        float userbuttons, btn2;                      // inspired by modwiki

        boolean already_set = ( false );              // each second "Z"-press will reverse this flag and reset g_fov

            while(1)
            {
            userbuttons = $player1.getButtons();
            btn2 = 4 & userbuttons;                   // 0000 0100 & 0000 0100 = true

              if(btn2)                                // 'Zoom' was pressed
                {
                     if( already_set )                // is it second press?
                     {
                        sys.setcvar( "g_fov", "90" );
                        already_set = !already_set;
                        debug_print();
                        sys.waitFrame();
                     }
                     else
                     {
                        sys.setcvar( "g_fov", "10" );
                        already_set = !already_set;
                        debug_print();
                     }
                }
            sys.waitFrame();
            }
        }

void main()
        {
        thread pressed_fov();       // always run when level is loaded
        }

« Last Edit: September 02, 2017, 01:13:53 PM by bitterman »

The Happy Friar

  • Happy Happy Joy Joy
  • Administrator
  • Hero Member
  • *****
  • Posts: 866
  • Karma: +42/-4
  • Semi-Newbie.
    • View Profile
    • EarthQuake
Re: Zooming by run loop script
« Reply #1 on: September 02, 2017, 02:45:42 PM »
Doom 3 has a built in zoom feature for each weapon. 
http://idtechforums.fuzzylogicinc.com/index.php?topic=260.0

"zoomFOV" in the weapon's def.

Would that make things easier?

argoon

  • Sr. Member
  • ****
  • Posts: 275
  • Karma: +21/-81
  • Doom Newbie
    • View Profile
Re: Zooming by run loop script
« Reply #2 on: September 02, 2017, 05:14:41 PM »
I trying to achieve a sniper zooming effect via 'g_fov' CVar.

It's realized via level script but as I think same algorithm can be used in weapon script.

The example below is works but from time to time g_fov is not set correct (stays in previous state or sets several times in one pass).

I thing it's because cycle checks is too fast.

Has anyone idea how to fix it?

Thanks.

Code: [Select]
void    debug_print()
        {
        sys.println( "^3g_fov set to: " + sys.getcvar( "g_fov" ) + " ^5at:" + sys.getTime() + "sec" );
        }

void    pressed_fov()
        {
        float userbuttons, btn2;                      // inspired by modwiki

        boolean already_set = ( false );              // each second "Z"-press will reverse this flag and reset g_fov

            while(1)
            {
            userbuttons = $player1.getButtons();
            btn2 = 4 & userbuttons;                   // 0000 0100 & 0000 0100 = true

              if(btn2)                                // 'Zoom' was pressed
                {
                     if( already_set )                // is it second press?
                     {
                        sys.setcvar( "g_fov", "90" );
                        already_set = !already_set;
                        debug_print();
                        sys.waitFrame();
                     }
                     else
                     {
                        sys.setcvar( "g_fov", "10" );
                        already_set = !already_set;
                        debug_print();
                     }
                }
            sys.waitFrame();
            }
        }

void main()
        {
        thread pressed_fov();       // always run when level is loaded
        }

I don't know but it could be because getbuttons runs more than one time at button press(was made for continuous pressing like shooting), no matter how fast you press it, so that perhaps is messing things up, before i add full access to impulses (they run only one time) this also caused many problems to me, you need to find a way to make the code run only one time at button press, unfortunately that is easier said than done.

bitterman

  • Sr. Member
  • ****
  • Posts: 270
  • Karma: +8/-5
  • Doom Newbie
    • View Profile
Re: Zooming by run loop script
« Reply #3 on: September 02, 2017, 09:15:33 PM »
Thanks guys.

Founds some interesting things:

Code: [Select]
idInterpolate<float> zoomFov;
Code: [Select]
/*
   82 ====================
   83 idInterpolate::Init
   84 ====================
   85 */
   86 template< class type >
   87 ID_INLINE void idInterpolate<type>::Init( const float startTime, const float duration, const type &startValue, const type &endValue ) {
   88         this->startTime = startTime;
   89         this->duration = duration;
   90         this->startValue = startValue;
   91         this->endValue = endValue;
   92         this->currentTime = startTime - 1;
   93         this->currentValue = startValue;
   94 }
Code: [Select]
if ( IsLocallyControlled() ) {
// zooming
if ( ( usercmd.buttons ^ oldCmd.buttons ) & BUTTON_ZOOM ) {
if ( ( usercmd.buttons & BUTTON_ZOOM ) && weapon.GetEntity() ) {
zoomFov.Init( gameLocal.time, 200.0f, CalcFov( false ), weapon.GetEntity()->GetZoomFov() );
} else {
zoomFov.Init( gameLocal.time, 200.0f, zoomFov.GetCurrentValue( gameLocal.time ), DefaultFov() );
}
}
}

Theoretically in my script  I can check delay with sys.getTime or just insert sys.wait(1) in cycle.
Is sys.wait is bad for performance in this case (it will be run in infinite loop)?
« Last Edit: September 03, 2017, 12:27:40 AM by bitterman »

argoon

  • Sr. Member
  • ****
  • Posts: 275
  • Karma: +21/-81
  • Doom Newbie
    • View Profile
Re: Zooming by run loop script
« Reply #4 on: September 03, 2017, 11:14:48 AM »
A while (1) loop is in it self a infinite loop unless you break from it, so be free to use sys.wait(1), btw a frame takes less than a second to run, if you open the doom_defs.script you will see that id software defined a frame time as 0.016 seconds (16 milliseconds), and i seam to recall that if you make a sys.wait() with less than 0.016 your loop will stop working.

About performance by using "thread" keyword before the function name you are making sure the loop doesn't clog the pipeline, also unless your loop is made of very complex chain's of if else's and many entity spawn arguments queries, like using many getkey() etc, than performance eating imo is not that bad, plus you should try to break from a while(1) loop ( break; ) if possible, unless it is required to run for the entirety of the game or map.

bitterman

  • Sr. Member
  • ****
  • Posts: 270
  • Karma: +8/-5
  • Doom Newbie
    • View Profile
Re: Zooming by run loop script
« Reply #5 on: September 04, 2017, 09:17:39 PM »
Thanks for useful info.

As say THF we can just add "zoomFov" "x" into appropriate weapon.def and it works just fine via Code (IMHO it set FOV from default to "x" and back to default interpolated between 200 ms).

And I see startFxFov native func and StopFxFov script event but it looks like usrealized feature.

And also InfluenceFov - don't know what is it yet.


The Happy Friar

  • Happy Happy Joy Joy
  • Administrator
  • Hero Member
  • *****
  • Posts: 866
  • Karma: +42/-4
  • Semi-Newbie.
    • View Profile
    • EarthQuake
Re: Zooming by run loop script
« Reply #6 on: September 05, 2017, 04:10:03 PM »
influenceFov is for when the influence trigger is done, one of the options is changing the player FOV.  It's all setup via the influence entity.