Snippet ASCII Object Id converter

NoobImbaPro

You can change this now in User CP.
Reaction score
60
Snippet:
JASS:
library ASCIIConverter /*v.2.50*/
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
//          ~|By NoobImbaPro|~          //
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
//
//  What does it do:
//      It typecasts an integer of type 'hfoo' into a string with "hfoo" value and the opposite
//
//  Where it can be used:
//      Many RPG maps hold a great amount of items,abilities,units etc...
//      So it can help them categorize and identify their items based on the letters of their object ids
//      to add some special properties to them.
//
//  API:
//      function ObjId2S takes integer ObjectId returns string
//      ------------------------------------------------------
//      function S2ObjId takes string ObjectId returns integer
//
// Installation:
//      Create a new trigger and rename it into ASCII Converter.
//      Convert it to custom text and paste this code inside it.
//
//  WE Bug:
//      You can't use the character "\" inside the integer id. Meaning this: '\abc'.
//      You can only type it in your object editor, not in trigger editor.
//
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
//          ~|By NoobImbaPro|~          //
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//

    globals
        private string alphabet = " !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~"
        private integer start   = 31 //Variable that skips the first characters that can't be used inside an object id integer
        private integer array value  //Explained on initialization function
        
        public string array index   //Simplify string results by isolating each letter from alphabet
    endglobals
    
    //Begining from the first letter from left side, identifies it and adds it into a string
    function ObjId2S takes integer ObjectId returns string
        local integer i = 0
        local integer j = 4
        local string result = ""
    
        loop
        exitwhen j == 0
            set i = R2I(ObjectId/value[j])
            set result = result + index[i-start]
            set ObjectId = ObjectId - i*value[j]
            set j = j - 1
        endloop
        
        return result
    endfunction
    
    function S2ObjId takes string ObjectId returns integer
        local integer length = StringLength(ObjectId)
        local integer result = 0
        local integer i = 0
        local integer j = 0
        local string objectPart
        
        if length == 4 then
            loop
                set objectPart = SubString(ObjectId, j, j + 1)
                loop
                    set i = i + 1
                    exitwhen index<i> == objectPart
                endloop
                set result = result + (i+start)*value[4-j]
                set j = j + 1
                set i = 0
            exitwhen j &gt; 4
            endloop
            
            return result
        else
    debug   call BJDebugMsg(&quot;Attempted to convert an invalid value into integer&quot;)
        endif
        
        return 0
    endfunction
    
    //Runs on Map initialization setting the &quot;values&quot; like the decimal ones.
    //Decimal 1 --&gt; 10 --&gt; 100 (10*10)  --&gt; 1000 (10*10*10)
    //ASCII   1 --&gt; 256 --&gt; 256 * 256 ....
    //With these values I can the parse each letter at a loop&#039;s repeat
    //In decimals the number 5678 is consisted by 5*1000 + 6*100 + 7*10 + 8*1
    //In ASCII the letter &quot;a&quot; has a value of 97 so an &#039;aaaa&#039; number is equal to 97*256*256*256 + 97*256*256 + 97*256 + 97
    module ASCII_Module_Initializer
        private static method onInit takes nothing returns nothing
            local integer i = 0
            
            loop
                set i = i + 1
                set index<i> = SubString(alphabet, i-1, i)
            exitwhen index<i> == &quot;&quot;
            endloop
            
            
            set value[1] = 1
            set value[2] = 256
            set value[3] = 256 * 256
            set value[4] = 256 * 256 * 256
        endmethod
    endmodule
    
    private struct ASCII_Struct_Module_Initializer
        implement ASCII_Module_Initializer
    endstruct
endlibrary</i></i></i>


Example:
JASS:
scope SpawnIllegalCharacterItem initializer onInit
// You can&#039;t preplace an item with this character &quot;\&quot; inside a &#039;abcd&#039; type variable.
// So we spawn the item with id &#039;\a$0&#039; by trigger on Map Initialization by converting it from a string witch uses the &quot;\&quot; character

    private function onInit takes nothing returns nothing
        call CreateItemLoc(S2ObjId(&quot;\\a$0&quot;), GetRectCenter(gg_rct_Region_000))
    endfunction
endscope


Change Log
Code:
v.1.00: Initial implement of ObjId2S function
v.2.00: Implement of S2ObjId function, faster coding of ObjId2S function
v.2.50: Globals initialization through module initializers
 

NoobImbaPro

You can change this now in User CP.
Reaction score
60
If you want it you use it. It has a certain purpose and it's made for that.
If Timer32 exists, does this mean KeyTimers2 have to retire? :D
 

NoobImbaPro

You can change this now in User CP.
Reaction score
60
@Nestharus
I tried to find something like that, but didn't, so I thought of making one...

*Any reason to use this over Ascii?

What do you mean??
 

inevit4ble

Well-Known Member
Reaction score
38
Straight up, I can see this code being way easy to read and being much more compact than the already existing systems.
 

Nestharus

o-o
Reaction score
84
It's not nearly as fast though


It also doesn't handle single ascii or convert from string back to ascii.
 

tooltiperror

Super Moderator
Reaction score
231
It's hard to make this stuff pretty. TheDamien's ASCII has the same O(1) complexity that this does.

To be honest, this is an awkward decision for me to make. But I have to say, with 2 Ascii snippets already approved, I can't approve one just on the grounds of readability.

Perhaps you could make a demomap showing many uses of Ascii functions, and then submit it with a brief introduction to how it works?
 

Nestharus

o-o
Reaction score
84
You do improper initialization. If I were to use this, it'd bug up my map ; P.

[ljass]library ASCIIConverter /*v.2.00*/ initializer Init[/ljass]

That type is a major no no. For this, you'd want a module initializer.


If you end up fixing this up and making it as good as possible, it'll just turn into a clone of Ascii that I did (one based on TheDamien's).


@tooltiperror

This does not have O(1) complexity.

Take a look at
[ljass]function S2ObjId takes string ObjectId returns integer[/ljass]

It loops through the string using SubString.
 

NoobImbaPro

You can change this now in User CP.
Reaction score
60
the second function, is handmade one, on next update I will do it better.

*library ASCIIConverter /*v.2.00*/ initializer Init

What's the problem exactly here? Because I didn't understand you
 

Laiev

Hey Listen!!
Reaction score
188
initializer is slowly compared to module init

so

this:

JASS:
library ASCIIConverter /*v.2.00*/ initializer Init

function init takes nothing returns nothing
endfunction


is slowly then

JASS:
module init
endmodule

struct initS
    implement init
endstruct
 

Magthridon96

Member
Reaction score
2
Actually Laiev, Just to be more clear:

JASS:
module Init
    private static method onInit takes nothing returns nothing
        // Put the Initialization code here
    endmethod
endmodule
private struct Inits extends array
    implement Init
endstruct


It's not about speed, it's about the order of execution.
Module Initializers run first, then struct initializers (onInit methods inside structs), then library/scope initializers, and finally InitTrig_ functions.
 

Laiev

Hey Listen!!
Reaction score
188
Sorry about that, I mean the order of initialization with that slowly :~

Thx Magthridon
 

tooltiperror

Super Moderator
Reaction score
231
@ Nestharus
You're right.

NoobImbaPro, any reason to use this over Ascii?
 

NoobImbaPro

You can change this now in User CP.
Reaction score
60
You did the same question, twice. And I don't understand you. Can you explain what you mean?
Should I had to do something else rather than ASCII from the first place?

@Magthridon96

Nice to know it. A lot of thanks.

A small question: I have integer i = 5/3, it will be inputed without problems and it will give 1?


If I keep S2ObjId same, isn't it good enough?? It's 100% accurate and doesn't need if/then/elses to cover some "bugs/exceptions"
 

Laiev

Hey Listen!!
Reaction score
188
The question is:

Why people should use THIS instead of the other Ascii?
 

Bribe

vJass errors are legion
Reaction score
67
>> A small question: I have integer i = 5/3, it will be inputed without problems and it will give 1?

Integers truncate in JASS when you use division. It is called Floor Division, which is why something
like ModuloInteger actually works.

If you want to make sure the division does not floor, you want to make sure one of those values
is a real number. 5./3 will show the correct result, for example.
 

NoobImbaPro

You can change this now in User CP.
Reaction score
60
No, I mean that I don't have to make this: integer i = R2I(5/3) right Bribe?


*Why people should use THIS instead of the other Ascii?

I searched and didn't find anything, so I thought of making one, but then I saw Nestharus showing bribe's ASCII snippet...

This forum's search should be really improved
 
General chit-chat
Help Users
  • No one is chatting at the moment.
  • 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