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.
  • Ghan Ghan:
    Still lurking
    +3
  • The Helper The Helper:
    I am great and it is fantastic to see you my friend!
    +1
  • The Helper The Helper:
    If you are new to the site please check out the Recipe and Food Forum https://www.thehelper.net/forums/recipes-and-food.220/
  • Monovertex Monovertex:
    How come you're so into recipes lately? Never saw this much interest in this topic in the old days of TH.net
  • Monovertex Monovertex:
    Hmm, how do I change my signature?
  • tom_mai78101 tom_mai78101:
    Signatures can be edit in your account profile. As for the old stuffs, I'm thinking it's because Blizzard is now under Microsoft, and because of Microsoft Xbox going the way it is, it's dreadful.
  • The Helper The Helper:
    I am not big on the recipes I am just promoting them - I use the site as a practice place promoting stuff
    +2
  • Monovertex Monovertex:
    @tom_mai78101 I must be blind. If I go on my profile I don't see any area to edit the signature; If I go to account details (settings) I don't see any signature area either.
  • The Helper The Helper:
    You can get there if you click the bell icon (alerts) and choose preferences from the bottom, signature will be in the menu on the left there https://www.thehelper.net/account/preferences
  • The Helper The Helper:
    I think I need to split the Sci/Tech news forum into 2 one for Science and one for Tech but I am hating all the moving of posts I would have to do
  • The Helper The Helper:
    What is up Old Mountain Shadow?
  • The Helper The Helper:
    Happy Thursday!
    +1
  • Varine Varine:
    Crazy how much 3d printing has come in the last few years. Sad that it's not as easily modifiable though
  • Varine Varine:
    I bought an Ender 3 during the pandemic and tinkered with it all the time. Just bought a Sovol, not as easy. I'm trying to make it use a different nozzle because I have a fuck ton of Volcanos, and they use what is basically a modified volcano that is just a smidge longer, and almost every part on this thing needs to be redone to make it work
  • Varine Varine:
    Luckily I have a 3d printer for that, I guess. But it's ridiculous. The regular volcanos are 21mm, these Sovol versions are about 23.5mm
  • Varine Varine:
    So, 2.5mm longer. But the thing that measures the bed is about 1.5mm above the nozzle, so if I swap it with a volcano then I'm 1mm behind it. So cool, new bracket to swap that, but THEN the fan shroud to direct air at the part is ALSO going to be .5mm to low, and so I need to redo that, but by doing that it is a little bit off where it should be blowing and it's throwing it at the heating block instead of the part, and fuck man
  • Varine Varine:
    I didn't realize they designed this entire thing to NOT be modded. I would have just got a fucking Bambu if I knew that, the whole point was I could fuck with this. And no one else makes shit for Sovol so I have to go through them, and they have... interesting pricing models. So I have a new extruder altogether that I'm taking apart and going to just design a whole new one to use my nozzles. Dumb design.
  • Varine Varine:
    Can't just buy a new heatblock, you need to get a whole hotend - so block, heater cartridge, thermistor, heatbreak, and nozzle. And they put this fucking paste in there so I can't take the thermistor or cartridge out with any ease, that's 30 dollars. Or you can get the whole extrudor with the direct driver AND that heatblock for like 50, but you still can't get any of it to come apart
  • Varine Varine:
    Partsbuilt has individual parts I found but they're expensive. I think I can get bits swapped around and make this work with generic shit though
  • Ghan Ghan:
    Heard Houston got hit pretty bad by storms last night. Hope all is well with TH.
  • The Helper The Helper:
    Power back on finally - all is good here no damage
    +2
  • V-SNES V-SNES:
    Happy Friday!
    +1
  • The Helper The Helper:
    New recipe is another summer dessert Berry and Peach Cheesecake - https://www.thehelper.net/threads/recipe-berry-and-peach-cheesecake.194169/

      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