Attaching to unit type ids

Zalinian

New Member
Reaction score
0
Sorry I couldn't be more descriptive in the title, but I wasn't sure how to word it.
edit: thanks for changing the title to a more appropriate wording.

Anyway here's the issue:
I originally had multiple arrays used to store units/heroes, the file path for their custom icons(used for my multiboard), bounty from killing them, and several other bits of data. I wanted to change this into a structure, since it will look more organized and make adding new units/heroes into the system, or even entire new branches of data easier. All of the data is set in the code and doesn't change.

For Example:

Instead of

unit array unit_list[2]
integer array unit_bounty[2]

set unit_list[1] = Peasant
set unit_list[2] = Footman
set unit_bounty[1] = 10
set unit_bounty[2] = 15

I would set up a struct:
JASS:
globals
   unit_data Peasant_Data
   unit_data Footman_Data
endglobals

struct unit_data
   integer unit (the unit's integer code)
   integer bounty
endstruct

Then in a function triggered on map initialization:
JASS:
// Peasant
set Peasant_Data.unit = (Peasant's unit integer code)
set Peasant_Data.bounty = 10
// Footman
set Footman_Data.unit = (Footman's unit integer code)
set Footman_Data.bounty = 15

I just need a way to look up the correct struct to obtain the data once I have the unit in question.

Is there a way to return data from a struct using its name in a variable? I can get the name of struct, but I can't get to the data if that makes any sense.
IE: A unit dies. {say the unit is a peon}
set name = GetUnitName(GetDyingUnit()) {this would return the string "Peon"}
The data I need I would have stored in Peon_Data. Is there a way to make that jump?

If anyone has any better ideas for storing my data, I'm open to suggestions.
 

Sevion

The DIY Ninja
Reaction score
413
Why don't you just store the unit type name in the struct.

JASS:
globals
   unit_data Peasant_Data
   unit_data Footman_Data
endglobals

struct unit_data
   integer unit (the unit's integer code)
   integer bounty
   string name
endstruct


JASS:
// Peasant
set Peasant_Data.unit = (Peasant's unit integer code)
set Peasant_Data.bounty = 10
set Peasant_Data.name = "Peasant"//GetUnitName(someunit)
// Footman
set Footman_Data.unit = (Footman's unit integer code)
set Footman_Data.bounty = 15
set Footman_Data.name = "Footman"//GetUnitName(someunit)
 

Zalinian

New Member
Reaction score
0
Why don't you just store the unit type name in the struct.

JASS:
globals
   unit_data Peasant_Data
   unit_data Footman_Data
endglobals

struct unit_data
   integer unit (the unit's integer code)
   integer bounty
   string name
endstruct


JASS:
// Peasant
set Peasant_Data.unit = (Peasant's unit integer code)
set Peasant_Data.bounty = 10
set Peasant_Data.name = "Peasant"//GetUnitName(someunit)
// Footman
set Footman_Data.unit = (Footman's unit integer code)
set Footman_Data.bounty = 15
set Footman_Data.name = "Footman"//GetUnitName(someunit)

I'm confused at how this is supposed to fix the problem :confused:

Use hashtable. Store the unit's code.
I've never used hashtables, or know anything about them. Would you happen to have a link to a tutorial or something please?
 

Sevion

The DIY Ninja
Reaction score
413
Hold on a second. Let me read again. I was distracted >_>

Edit: Yeah. I have no idea wtf I was thinking. Use a Hashtable with keys or a StringHash of the unit names. [ljass]StoreInteger(HASHTABLE, StringHash(GetUnitName(UNIT)), 0, BOUNTY)[/ljass]

Or maybe I'm just being stupid again. I'm really tired >_> And my contacts are raping my eyes.
 

Jesus4Lyf

Good Idea™
Reaction score
397
Based off this:
JASS:
struct TypeData extends array
    integer bounty
    
    //===================================
    // Jesus4Lyf's Hash function.
    private static constant integer HASH_NEXT=53
    private static constant integer MAX_HASH_VALUE=8191
    private static integer array HashedInt
    static method operator [] takes integer int returns thistype
        local integer hash=int-(int/thistype.MAX_HASH_VALUE)*thistype.MAX_HASH_VALUE
        loop
            exitwhen thistype.HashedInt[hash]==int
            if thistype.HashedInt[hash]==0 then
                set thistype.HashedInt[hash]=int
                return thistype(hash)
            endif
            set hash=hash+thistype.HASH_NEXT
            if hash>=thistype.MAX_HASH_VALUE then
                set hash=hash-thistype.MAX_HASH_VALUE
            endif
        endloop
        return thistype(hash)
    endmethod
endstruct

// Example:
local TypeData d=TypeData['hfoo']
set d.bounty=50

local TypeData d=TypeData[GetUnitTypeId(GetTriggerUnit())]
call BJDebugMsg(I2S(d.bounty))

I don't know if it compiles, but this should be about what you want.
If the extending array thing is a problem, I can solve it. But hopefully, it isn't. :)
 

Zalinian

New Member
Reaction score
0
Based off this:
JASS:
struct TypeData extends array
    integer bounty
    
    //===================================
    // Jesus4Lyf's Hash function.
    private static constant integer HASH_NEXT=53
    private static constant integer MAX_HASH_VALUE=8191
    private static integer array HashedInt
    static method operator [] takes integer type returns thistype
        local integer hash=int-(int/thistype.MAX_HASH_VALUE)*thistype.MAX_HASH_VALUE
        loop
            exitwhen thistype.HashedInt[hash]==int
            if thistype.HashedInt[hash]==0 then
                set thistype.HashedInt[hash]=int
                return thistype(hash)
            endif
            set hash=hash+thistype.HASH_NEXT
            if hash>=thistype.MAX_HASH_VALUE then
                set hash=hash-thistype.MAX_HASH_VALUE
            endif
        endloop
        return thistype(hash)
    endfunction
endstruct

// Example:
local TypeData d=TypeData['hfoo']
set d.bounty=50

local TypeData d=TypeData[GetUnitTypeId(GetTriggerUnit())]
call BJDebugMsg(I2S(d.bounty))

I don't know if it compiles, but this should be about what you want.
If the extending array thing is a problem, I can solve it. But hopefully, it isn't. :)

I'm not totally sure if that does it since I've never dealt with hashtables before. Would someone know a good guide/tutorial for it?
 

Sevion

The DIY Ninja
Reaction score
413
What Jesus is doing is giving you a struct array that is indexed by the unit's ID. So the struct instance for a Footman is [ljass]TypeData['hfoo'][/ljass]
 

Jesus4Lyf

Good Idea™
Reaction score
397
>I'm not totally sure if that does it since I've never dealt with hashtables before
It doesn't use hashtables...
Just refer to the example code I posted?

If you follow this link all your problems should be solved, either way...
It allows you to call [LJASS]Hash(unitID)[/LJASS] to convert it to a valid array index that won't clash, and then you can attach a struct using an array...
You can do it with hashtables if you prefer.

Edit: Don't get me wrong, it is exactly what you're after if it compiles, and the code is well tested and works (if it compiles). You don't need to check it. :)
 

Zalinian

New Member
Reaction score
0
Ah sry, I read it too fast. I'll look into it more.

edit: Alright. I don't totally understand it all just yet, but I'm figuring it out. Thanks for the help. :)

edit:
compile errors. I think I caught them.
JASS:
struct TypeData extends array
    integer bounty
    
    //===================================
    // Jesus4Lyf's Hash function.
    private static constant integer HASH_NEXT=53
    private static constant integer MAX_HASH_VALUE=8191
    private static integer array HashedInt
    static method operator [] takes integer type returns thistype // <-- takes integer int
        local integer hash=int-(int/thistype.MAX_HASH_VALUE)*thistype.MAX_HASH_VALUE
        loop
            exitwhen thistype.HashedInt[hash]==int
            if thistype.HashedInt[hash]==0 then
                set thistype.HashedInt[hash]=int
                return thistype(hash)
            endif
            set hash=hash+thistype.HASH_NEXT
            if hash>=thistype.MAX_HASH_VALUE then
                set hash=hash-thistype.MAX_HASH_VALUE
            endif
        endloop
        return thistype(hash)
    endfunction // <-- endmethod
endstruct

It compiles now, testing to see if it works correctly.

Edit...again: Works like a charm.
 

kingkingyyk3

Visitor (Welcome to the Jungle, Baby!)
Reaction score
216
JASS:

library UnitTypeData

    struct UnitTypeData extends array
        private static hashtable ht = InitHashtable()
        
        //! textmacro UNIT_TYPE_DATA takes TYPE, DATA, FUNCTION
        private static key $DATA$Key
        
        method operator $DATA$= takes $TYPE$ which$DATA$ returns nothing
            call Save$FUNCTION$(thistype.ht,this,$DATA$Key,which$DATA$)
        endmethod
        
        method operator $DATA$ takes nothing returns $TYPE$
            return Load$FUNCTION$(thistype.ht,this,$DATA$Key)
        endmethod
        //! endtextmacro
        
        //! runtextmacro UNIT_TYPE_DATA("string", "Name", "Str")
        //! runtextmacro UNIT_TYPE_DATA("integer", "Bounty", "Integer")

    endstruct
    
endlibrary


This has simpler interface. If you want new type of data, use the textmacro.
This is faster than Hash when Hash meets collision.

JASS:

    function Test takes nothing returns nothing
        set UnitTypeData['hpea'].Name = "I am a peasant."
        set UnitTypeData['hpea'].Bounty = 10
    endfunction
 

eXirrah

New Member
Reaction score
51
When I do things like this I always use 1 array and the units type id's for indexes.

for example

JASS:
globals
    integer array UnitsBounty
endglobals
 //footman = 'u000'
// peasant = 'u001'
// knight  = 'u002'
//.....

function UnitDies takes nothing returns nothing
    local player p = GetOwningPlayer(GetDyingUnit())
    local integer i = GetUnitTypeId(GetDyingUnut()) - 'u000'
    local integer g = GetPlayerState(p,PLAYER_STATE_RESOURCE_GOLD)
    call SetPlayerState(p,PLAYER_STATE_RESOURCE_GOLD,g+UnitsBounty<i>)
endfunction

function OnInit takes nothing returns nothing
    set UnitsBounty[0] = 15 // footman bounty &#039;u000&#039;-&#039;u000&#039; = 0
    set UnitsBounty[1] = 20 // peasant bounty &#039;u001&#039;-&#039;u000&#039; = 1
    set UnitsBounty[2] = 25 // knight bounty &#039;u002&#039;-&#039;u000&#039; = 2 
endfunction
</i>


EDIT: As Jesus4Lyf reminded me in order to use my system of typeid indexing you
need to use Jass NewGen pack with enabled Grimoire -> Enable Object Editor Hack
 
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