Discussion Getting accurate results, with converting reals to integers ?

Komaqtion

You can change this now in User CP.
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'll also have to update JASS Helper to the latest version...                 @@//
//@@               And, then you'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" 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" 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" data-shortname=":D" />                                                    @@//
//@@                                                                                               @@//
//@@               And credits, if you use this that is, is not needed to give me                  @@//
//@@               though it's always welcome <img src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" class="smilie smilie--sprite smilie--sprite2" alt=";)" title="Wink    ;)" 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" 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.
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.
Well, that doesn't cut it...
I still need integers each period, to add/remove color from the unit...
 
General chit-chat
Help Users
  • No one is chatting at the moment.
  • The Helper The Helper:
    Is that going to be a full game now tom?
  • The Helper The Helper:
    what is this Ghan? https://enterprise.ghannet.com/ you are totally holding out on us my friend
  • Ghan Ghan:
    Not mine, I just ran the server. They let the domain expire but it was a cool project so I kept it alive there.
  • Ghan Ghan:
    Used to be enterpriserpg.com I think.
  • The Helper The Helper:
    If you go to the world editor tutorials site and click the starcraft link it takes you there :)
  • Ghan Ghan:
    lol
  • Ghan Ghan:
    I forgot about that alias name. (The full URL still went to the correct site.) That should be fixed.
  • Ghan Ghan:
    Silkroad Forums site really needs help.
  • Ghan Ghan:
    Its software has been out of support since 2017.
  • Ghan Ghan:
    The woes of using custom styles....
  • The Helper The Helper:
    does anyone still use silk road forums?
  • Ghan Ghan:
    It's about as active as here I'd say. Not much going on.
  • Wizard Wizard:
    I think it doesn't help that the silk road game isn't as popular any more I think. The only MMO I play these days is FFXIV like most other people I know.
  • The Helper The Helper:
    I checked a status page for players and it says the game has 500 daily players.
  • The Helper The Helper:
    I cannot believe that site is still going I wonder if Ryoko is involved with it at all would love to talk to him again and see what he is up to
  • The Helper The Helper:
    my oldest daughter failed her driving test today for her license she is 22 and yes I have been driving her everywhere her whole life
  • The Helper The Helper:
    Now they are blaming me on Facebook for her failure LOL I am accepting it because it is my failure I am her driving teacher even though she had certified driving instruction from the best place in town I for some reason cannot teach her how to drive this is frustrating
  • jonas jonas:
    I didn't get my license until I was 25
    +1
  • The Helper The Helper:
    Back in the old days when I was getting a license people were getting them at 16 yrs old and many were getting hardships so they could get them earlier. Almost nobody did not drive when they were able. Nowadays though....
  • Ghan Ghan:
    Why drive when there's Uber Eats?
    +2
  • The Helper The Helper:
    I hope Varine is OK have not heard from him in a while.
    +1
  • Ghan Ghan:
    Diablo 2 Resurrected launching in 9 days.
  • The Helper The Helper:
    Just got a new to me computer hope it will run it
  • jonas jonas:
    I would buy if it had better modding support than D2, like a small lua script engine or much less hardcoded stuff

    Members online

    Affiliates

    Hive Workshop NUON Dome
    Top