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.
  • Varine Varine:
    How can you tell the difference between real traffic and indexing or AI generation bots?
  • The Helper The Helper:
    The bots will show up as users online in the forum software but they do not show up in my stats tracking. I am sure there are bots in the stats but the way alot of the bots treat the site do not show up on the stats
  • Varine Varine:
    I want to build a filtration system for my 3d printer, and that shit is so much more complicated than I thought it would be
  • Varine Varine:
    Apparently ABS emits styrene particulates which can be like .2 micrometers, which idk if the VOC detectors I have can even catch that
  • Varine Varine:
    Anyway I need to get some of those sensors and two air pressure sensors installed before an after the filters, which I need to figure out how to calculate the necessary pressure for and I have yet to find anything that tells me how to actually do that, just the cfm ratings
  • Varine Varine:
    And then I have to set up an arduino board to read those sensors, which I also don't know very much about but I have a whole bunch of crash course things for that
  • Varine Varine:
    These sensors are also a lot more than I thought they would be. Like 5 to 10 each, idk why but I assumed they would be like 2 dollars
  • Varine Varine:
    Another issue I'm learning is that a lot of the air quality sensors don't work at very high ambient temperatures. I'm planning on heating this enclosure to like 60C or so, and that's the upper limit of their functionality
  • Varine Varine:
    Although I don't know if I need to actually actively heat it or just let the plate and hotend bring the ambient temp to whatever it will, but even then I need to figure out an exfiltration for hot air. I think I kind of know what to do but it's still fucking confusing
  • The Helper The Helper:
    Maybe you could find some of that information from AC tech - like how they detect freon and such
  • Varine Varine:
    That's mostly what I've been looking at
  • Varine Varine:
    I don't think I'm dealing with quite the same pressures though, at the very least its a significantly smaller system. For the time being I'm just going to put together a quick scrubby box though and hope it works good enough to not make my house toxic
  • Varine Varine:
    I mean I don't use this enough to pose any significant danger I don't think, but I would still rather not be throwing styrene all over the air
  • The Helper The Helper:
    New dessert added to recipes Southern Pecan Praline Cake https://www.thehelper.net/threads/recipe-southern-pecan-praline-cake.193555/
  • The Helper The Helper:
    Another bot invasion 493 members online most of them bots that do not show up on stats
  • Varine Varine:
    I'm looking at a solid 378 guests, but 3 members. Of which two are me and VSNES. The third is unlisted, which makes me think its a ghost.
    +1
  • The Helper The Helper:
    Some members choose invisibility mode
    +1
  • The Helper The Helper:
    I bitch about Xenforo sometimes but it really is full featured you just have to really know what you are doing to get the most out of it.
  • The Helper The Helper:
    It is just not easy to fix styles and customize but it definitely can be done
  • The Helper The Helper:
    I do know this - xenforo dropped the ball by not keeping the vbulletin reputation comments as a feature. The loss of the Reputation comments data when we switched to Xenforo really was the death knell for the site when it came to all the users that left. I know I missed it so much and I got way less interested in the site when that feature was gone and I run the site.
  • Blackveiled Blackveiled:
    People love rep, lol
    +1
  • The Helper The Helper:
    The recipe today is Sloppy Joe Casserole - one of my faves LOL https://www.thehelper.net/threads/sloppy-joe-casserole-with-manwich.193585/
  • The Helper The Helper:
    Decided to put up a healthier type recipe to mix it up - Honey Garlic Shrimp Stir-Fry https://www.thehelper.net/threads/recipe-honey-garlic-shrimp-stir-fry.193595/

      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