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 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