Discussion Getting accurate results, with converting reals to integers ?

Komaqtion

You can change this now in User CP.
Reaction score
469
Hello !

Ok, so I'm about to update the little snippet/system I've created called SmoothUnitModification and one "bug"/tweak I need to fix is that it's extremely inaccurate, s in if I want to remove 150 of a units red color (Out of the 255 is has as default) I'll end up with 129, when I should have gotten 105...

Now, I know this is because I have to use reals as the "incrementation" (Is this even a word ? :S) because thay are so low, and the native which changes the color takes only integers...

So, I came up with a quite extensive formula to do this VERY accurately even if it needs to be converted from real to integers and this was done by having a second increment variable for each value, which had the same value as the other variable, only 1. closer to 0.
Anyways, I'll show the script first, and explain below :D

JASS:
library SmoothUnitMod requires T32, UnitAppearance

///////////////////////////////////////////////////////////////////////////////////////////////////////
//***************************************************************************************************//
//@@//////////////////////////////// *//SmoothUnitModification\\* /////////////////////////////////@@//
//@@                                                                                               @@//
//@@                                                                                               @@//
//@@           %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%            @@//
//@@           %%                                                                    %%            @@//
//@@           %%        /////////     ///          \\\   ///\\\            ///\\\   %%            @@//
//@@           %%      /////    ////   ///          \\\   ///\\\            ///\\\   %%            @@//
//@@           %%     /////            ///          \\\   /// \\\          /// \\\   %%            @@//
//@@           %%    /////             ///          \\\   /// \\\          /// \\\   %%            @@//
//@@           %%     /////            ///          \\\   ///  \\\        ///  \\\   %%            @@//
//@@           %%      /////           ///          \\\   ///  \\\        ///  \\\   %%            @@//
//@@           %%       ////\\\\       ///          \\\   ///   \\\      ///   \\\   %%            @@//
//@@           %%           \\\\\      ////        \\\\   ///   \\\      ///   \\\   %%            @@//
//@@           %%            \\\\\     ////        \\\\   ///    \\\    ///    \\\   %%            @@//
//@@           %%              \\\\    ////        \\\\   ///    \\\    ///    \\\   %%            @@//
//@@           %%            \\\\\      ////      \\\\    ///     \\\  ///     \\\   %%            @@//
//@@           %%   \\\\    \\\\\        ////    \\\\     ///     \\\  ///     \\\   %%            @@//
//@@           %%      \\\\\\\\             ///\\\        ///      \\\///      \\\   %%            @@//
//@@           %%                                                                    %%            @@//
//@@           %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%            @@//
//@@                                                                                               @@//
//@@                                                                                               @@//
//@@                                                                                               @@//
//@@                                                                                               @@//
//@@                                    Made by , Komaqtion                                        @@//
//@@                                                                                               @@//
//@@                                                                                               @@//
//@@                                          Purpose:                                             @@//
//@@                                                                                               @@//
//@@             ¤ This is a system, which will give the user the opportunity to easily modify     @@//
//@@               3 main aspects of units: Scale, ARGB color and Time Scale (Animation Speed).    @@//
//@@                                                                                               @@//
//@@                                           Usage:                                              @@//
//@@                                                                                               @@//
//@@             ¤ The function that this system provides the user are:                            @@//
//@@                                                                                               @@//
//@@             function ScaleSimple takes unit Scaler, real Time, real scaleMod                  @@//
//@@             function ScaleEx takes unit Scaler, real Time, real scaleMod, code userFunc       @@//
//@@                                                                                               @@//
//@@               "Scaler" is the unit which is to be scaled.                                     @@//
//@@               "Time" is basically, over how long time will the unit scale...                  @@//
//@@               "scaleMod" is how much the unit's scale will change, example:                   @@//
//@@               "userFunc" is the callback, if wanted, the system will execute                  @@//
//@@               when the scaling is done.                                                       @@//
//@@               Example:                                                                        @@//
//@@                                                                                               @@//
//@@               call ScaleSimple( GetTriggerUnit(), 2.0, 0.5 )                                  @@//
//@@                                                                                               @@//
//@@               Will scale the unit to +50% over 2 seconds.                                     @@//
//@@               And, also the modification is in decimal form, meaning that 1.0 will            @@//
//@@               double the unit's size...                                                       @@//
//@@                                                                                               @@//
//@@               The timescale part of this system is basically the same as this, and uses       @@//
//@@               the same amount of "takes"...                                                   @@//
//@@                                                                                               @@//
//@@               The fade part, on the other hand, has more "takes", simply because it handles   @@//
//@@               the red, green and blue colors too, and not only transparency...                @@//
//@@               An example:                                                                     @@//
//@@                                                                                               @@//
//@@               call FadeEX( GetTriggerUnit(), 2.0, 0.0, -255, -255, -127.5 )                   @@//
//@@                                                                                               @@//
//@@               This little sample will make the unit 50% transparent, and also RED.            @@//
//@@               And all the arguments here ranges from 0.0 - 255.0, meaning that a 50%          @@//
//@@               transparent unit has an alpha value of 127.5...                                 @@//
//@@                                                                                               @@//
//@@               You can also use this system to "get" any of the values you can change with     @@//
//@@               it, using functions like this:                                                  @@//
//@@                                                                                               @@//
//@@               function GetUnitScale takes unit u returns real                                 @@//
//@@                                                                                               @@//
//@@               Which will give you the current scale of the unit...                            @@//
//@@                                                                                               @@//
//@@                                           Note:                                               @@//
//@@                                                                                               @@//
//@@             ¤ This system does not support pre-changed RGB colors from the Object Editor,     @@//
//@@               and I'd suggest you manually change the color of the units in-game instead.     @@//
//@@               It presumes that all colors of all units are set to 255 from the start !        @@//
//@@                                                                                               @@//
//@@                                       Requirements:                                           @@//
//@@                                                                                               @@//
//@@             ¤ A vJASS preprocessor, which is easiest found by downloading JASS Newgen Pack    @@//
//@@               <a href="http://www.thehelper.net/forums/showthread.php?t=73936" class="link link--internal">http://www.thehelper.net/forums/showthread.php?t=73936</a>                          @@//
//@@               You&#039;ll also have to update JASS Helper to the latest version...                 @@//
//@@               And, then you&#039;ll need the system called Timer32 (T32), and also my other        @@//
//@@               system, called &quot;UnitAppearance&quot; <img src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" class="smilie smilie--sprite smilie--sprite8" alt=":D" title="Big Grin    :D" loading="lazy" data-shortname=":D" />                                              @@//
//@@                                                                                               @@//
//@@                                         Credits:                                              @@//
//@@                                                                                               @@//
//@@             ¤ Viikuna, for helping me out, and also helping me make &quot;UnitAppearance&quot; !        @@//
//@@               Jesus4Lyf, for also helping me out alot, and making Timer 32 ! <img src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" class="smilie smilie--sprite smilie--sprite8" alt=":D" title="Big Grin    :D" loading="lazy" data-shortname=":D" />               @@//
//@@               Azlier, for some help too <img src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" class="smilie smilie--sprite smilie--sprite8" alt=":D" title="Big Grin    :D" loading="lazy" data-shortname=":D" />                                                    @@//
//@@                                                                                               @@//
//@@               And credits, if you use this that is, is not needed to give me                  @@//
//@@               though it&#039;s always welcome <img src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" class="smilie smilie--sprite smilie--sprite2" alt=";)" title="Wink    ;)" loading="lazy" data-shortname=";)" />                                                   @@//
//@@                                                                                               @@//
//@@///////////////////////////////////////////////////////////////////////////////////////////////@@//
//***************************************************************************************************//
///////////////////////////////////////////////////////////////////////////////////////////////////////

    struct SmoothScaleChange
    
        private integer ticks
        
        private real increment
        
        private unit u
        
        private method periodic takes nothing returns nothing
            set ticks = ticks - 1
            
            call ModifyUnitScale( u, increment ) // This will save the scale of the unit in a struct using AIDS...
            
            debug call DisplayTimedTextToPlayer( GetLocalPlayer(), 0., 0., 6000., &quot;#SUM# - Scale: &quot; + R2S( Appearance[ u ].scale ) )
            
            if ticks &lt;= 0 then // When ticks becomes 0 (Meaning that the scaling is done) destroy it, and remove it from the T32 queue ! <img src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" class="smilie smilie--sprite smilie--sprite8" alt=":D" title="Big Grin    :D" loading="lazy" data-shortname=":D" />
                call stopPeriodic()
                
                call destroy()
            endif
            
        endmethod
        
        implement T32x
        
        static method create takes unit Modifier, real Time, real scaleMod returns thistype
            local thistype this = thistype.allocate()
            
            set u = Modifier // Which unit to scale XD
            
            set ticks = R2I( Time / T32_PERIOD ) // How many times the timer must fire...
            set increment = scaleMod / ticks // How much the scale will increase each PERIOD...
            
            call startPeriodic()
            
            return this
        endmethod
        
    endstruct
    
    function ScaleUnit takes unit Modifier, real Time, real scaleMod returns SmoothScaleChange
        return SmoothScaleChange.create( Modifier, Time, scaleMod )
    endfunction
    
    function ScaleUnitValue takes unit Modifier, real Time, real scaleValue returns SmoothScaleChange
        return SmoothScaleChange.create( Modifier, Time, scaleValue - Appearance[ Modifier ].scale )
    endfunction
    
    function GetUnitScale takes unit u returns real
        return Appearance[ u ].scale
    endfunction
    
    struct SmoothColorChange
    
        private integer ticks
        
        private real rincrement
        private real secondrinc
        private real gincrement
        private real secondginc
        private real bincrement
        private real secondbinc
        private real increment
        private real secondinc
        
        private unit u
        
        private method periodic takes nothing returns nothing
            set ticks = ticks - 1
        
            call ModifyUnitVertexColor( u, R2I( rincrement ), R2I( gincrement ), R2I( bincrement ), R2I( increment ) )
            
            debug call DisplayTimedTextToPlayer( GetLocalPlayer(), 0., 0., 6000., &quot;#SUM# - Red: &quot; + R2S( Appearance<u>.red ) )
            debug call DisplayTimedTextToPlayer( GetLocalPlayer(), 0., 0., 6000., &quot;#SUM# - Green: &quot; + R2S( Appearance<u>.green ) )
            debug call DisplayTimedTextToPlayer( GetLocalPlayer(), 0., 0., 6000., &quot;#SUM# - Blue: &quot; + R2S( Appearance<u>.blue ) )
            debug call DisplayTimedTextToPlayer( GetLocalPlayer(), 0., 0., 6000., &quot;#SUM# - Alpha: &quot; + R2S( Appearance<u>.alpha ) )
            
            if ticks &lt;= 0 then
                call destroy()
                
                call stopPeriodic()
            endif
            
        endmethod
        
        implement T32x
        
        static method create takes unit Modifier, real Time, real redMod, real greenMod, real blueMod, real alphaMod returns thistype
            local thistype this = thistype.allocate()
            
            set ticks = R2I( Time / T32_PERIOD )
            
            set u = Modifier
            
            set increment = alphaMod / ticks
            set rincrement = redMod / ticks
            set gincrement = greenMod / ticks
            set bincrement = blueMod / ticks
            
            call startPeriodic()
            
            return this
        endmethod
        
    endstruct
    
    function FadeUnit takes unit Modifier, real Time, real alphaMod returns SmoothColorChange
        return SmoothColorChange.create( Modifier, Time, 0., 0., 0., alphaMod )
    endfunction
    
    function FadeUnitARGB takes unit Modifier, real Time, real redMod, real greenMod, real blueMod, real alphaMod returns SmoothColorChange
        return SmoothColorChange.create( Modifier, Time, redMod, greenMod, blueMod, alphaMod )
    endfunction
    
    function GetUnitColorAlpha takes unit u returns integer
        return R2I( Appearance[ u ].alpha )
    endfunction

    function GetUnitColorRed takes unit u returns integer
        return R2I( Appearance[ u ].red )
    endfunction
    
    function GetUnitColorGreen takes unit u returns integer
        return R2I( Appearance[ u ].green )
    endfunction
    
    function GetUnitColorBlue takes unit u returns integer
        return R2I( Appearance[ u ].blue )
    endfunction
    
    struct SmoothTSChange
    
        private integer ticks
        
        private real increment
        
        private unit u
        
        private method periodic takes nothing returns nothing
            set ticks = ticks - 1
        
            call ModifyUnitTimeScale( u, increment )
            
            debug call DisplayTimedTextToPlayer( GetLocalPlayer(), 0., 0., 6000., &quot;#SUM# - Timescale :&quot; + R2S( Appearance[ u ].timescale ) )
            
            if ticks &lt;= 0 then
                call destroy()
                
                call stopPeriodic()
            endif
            
        endmethod
        
        implement T32x
        
        static method create takes unit Modifier, real Time, real timescaleMod returns thistype
            local thistype this = thistype.allocate()
            
            set u = Modifier
            
            set ticks = R2I( Time * T32_PERIOD ) // How many times the timer must fire...
            set increment = timescaleMod / ticks
            
            call startPeriodic()
            
            return this
        endmethod
        
    endstruct
    
    function ChangeTimeScale takes unit Modifier, real Time, real timescaleMod returns SmoothTSChange
        return SmoothTSChange.create( Modifier, Time, timescaleMod )
    endfunction
    
    function GetUnitTimeScale takes unit u returns real
        return Appearance[ u ].timescale
    endfunction

endlibrary</u></u></u></u>


(Note: The documentation isn't done yet :p)

JASS:
library UnitAppearance requires AIDS

    struct Appearance extends array
        //! runtextmacro AIDS()

        real scale
        
        real timescale
        
        real alpha
        real red
        real green
        real blue
        
        private method AIDS_onCreate takes nothing returns nothing
            set this.scale = 1.0
            
            set this.alpha = 255.0
            set this.red = 255.0
            set this.green = 255.0
            set this.blue = 255.0
            
            set this.timescale = 1.0
        endmethod
        
    endstruct

    function ModifyUnitScale takes unit u, real addscale returns nothing
        local Appearance this = Appearance[ u ]
        
        if this.scale &gt; 0 then
            set this.scale = this.scale + addscale
            
            call SetUnitScale( this.unit, this.scale, 0.,0. )
        endif
        
    endfunction
    
    function ModifyUnitVertexColor takes unit u, real addred, real addgreen, real addblue, real addalpha returns nothing
        local Appearance this = Appearance[ u ]
        
        if this.alpha &gt; 0 then
            set this.alpha = this.alpha + addalpha
        endif
        
        if this.red &gt; 0 then
            set this.red = this.red + addred
        endif
        
        if this.green &gt; 0 then
            set this.green = this.green + addgreen
        endif
        
        if this.blue &gt; 0 then
            set this.blue = this.blue + addblue
        endif
        
        if this.alpha &gt; 0 or this.red &gt; 0 or this.green &gt; 0 or this.blue &gt; 0 then
            call SetUnitVertexColor( this.unit, R2I( this.red ), R2I( this.green ), R2I( this.blue ), R2I( this.alpha ) )
        endif
        
    endfunction
    
    function ModifyUnitTimeScale takes unit u, real addtimescale returns nothing
        local Appearance this = Appearance[ u ]
        
        if this.timescale &gt; 0 then
            set this.timescale = this.timescale + addtimescale
        
            call SetUnitTimeScale( this.unit, this.timescale )
        endif
        
    endfunction
    
endlibrary


Now, lets say I call the 'Fade' function like this:
[ljass]call FadeUnitARGB( u, 10., -100., 0., 0., 0. )[/ljass]

That means that I want to reduce the red by 100.0, meaning almost by 50% and the endresult should be 155.0.

Though, as the script currently is, this will not be the result as it will most likely be much less than 155.0, but if I were to replace a few period inbetween somewhere with an increment which is 1 less than the main one, that'd mean that I would get a slightly better result.

Now, I came up with the perfect formula for this (As I've said) but I didn't do it in the script itself, and I used weird variable names, so I couldn't really implement it into the system, but I could only test it in the function I created it and it worked flawlessly...

Now the problem I'm having now is that I accidentaly deleted that small bit of code from my map, and can't get it back now :(
But I remember it had something to do with isolating the decimals from the variable, and then determining how many times it was needed to use the real increment, and also the increment which had the -1.0 in value...

I'm really sorry if I've just blown up your minds with this nonsense of explanation, but it's the best I can do as I didn't actually completely understand what I did XD

But any ideas, or maybe help to explain it better to others if you understand my explenation a bit would be GREATLY appreciated :D
 

saw792

Is known to say things. That is all.
Reaction score
280
Wouldn't it be better to just multiply the real value by 10000 or something, then decrement it as an integer and only convert to a real where it needs to be real?
 

Komaqtion

You can change this now in User CP.
Reaction score
469
Well, that doesn't cut it...
I still need integers each period, to add/remove color from the unit...
 

Komaqtion

You can change this now in User CP.
Reaction score
469
Why would it ? :eek:
I've used it and it has never crashed...
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Members online

      No members online now.

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top