• 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!
  • It's time for the first HD Modeling Contest of 2024. Join the theme discussion for Hive's HD Modeling Contest #6! Click here to post your idea!

[vJASS] Table

Status
Not open for further replies.

Kazeon

Hosted Project: EC
Level 34
Joined
Oct 12, 2011
Messages
3,449
Guys, I'm new vJasser and still learning how to use Bribe's table. Here is my code
JASS:
library SRS initializer Ini
    /*****************************************************************************************************/
    /**                                                                                                 **/
    /**                                 'Safe Revive System v1.4'                                       **/
    /**                                                     *****                                       **/
    /**                                                by Dalvengyr aka .....                           **/
    /**                                                                                                 **/
    /*  Why safe? This revive system allows you to revive any organic non-Hero units without recreating  */
    /*  them. In the demo map there will be shown floating shits that shows units handle that is not     */
    /*  changed after revived. What are the advantages of using this system? This method is faster than  */
    /*  recreating new unit, and save spaces in memory. The only thing you need to do is set units death */
    /*  type to 'can raise, does decay'.                                                                 */
    /*                                                                                                   */
    /*  Requirements:                                                                                    */
    /*      - Table                                                                                      */
    /*      - RegAnyUnitEvent                                                                            */
    /*      - Any unit indexer                                                                           */
    /*                                                                                                   */
    /*  Disadvantages:                                                                                   */
    /*      - None                                                                                       */
    /*                                                                                                   */
    /*  Implementation:                                                                                  */
    /*      1. Copy SRS folder into your map                                                             */
    /*      2. Copy dummy unit and spell into your map                                                   */
    /*      3. Make sure dummy and spell id below is the same with their rawcode at OE                   */
    /*      4. If you have dummy.mdx in your map, then open OE, set dummy units file model to that       */
    /*         dummy.mdx is better                                                                       */
    /*      5. Nuff said. Done.                                                                          */
    /*                                                                                                   */
    /*  APIs:                                                                                            */
    /*  1. Revive any unit you want at their current position                                            */
    /*                                                                                                   */
    /*  function ReviveUnit takes unit whichUnit returns boolean                                         */
    /*                                                                                                   */
    /*  2. Revive any unit you want at certain point                                                     */
    /*     Set boolean flag to true to check target point pathability                                    */
    /*     Keep in mind that checking pathability is always slower than not                              */
    /*                                                                                                   */
    /*  function ReviveUnitAtPoint takes unit whichUnit, real x, real y, boolean flag returns boolean    */
    /**                                                                                                 **/
    /*****************************************************************************************************/
    /*****************************************************************************************************/
    
    globals
    
    /* 'CONFIGURATIONS
       1. Dummy Id */
        private constant    integer     DUMMY_ID     = 'SRSd'
        
    /* 2. Spell Id */
        private constant    integer     SPELL_ID     = 'SRSa'
        
    /* 3. Ressurection Order Id */
        private constant    integer     ORDER_ID     = 852094
        
    /* 4. Size of table */
        private constant    integer     SIZE         = 8192
        
    /* 'END-OF-CONFIGURATIONS' */
    
        private             unit        DUMMY
        
    endglobals
        
    private function filter takes unit u returns boolean
        return not IsUnitType(u, UNIT_TYPE_STRUCTURE) and not IsUnitType(u, UNIT_TYPE_MECHANICAL) and not IsUnitType(u, UNIT_TYPE_HERO)
    endfunction

    private struct sr
        Table array tab[SIZE]
        
        static method save takes unit u returns nothing
            local integer h
            local thistype this
            
            set h = GetUnitUserData(u)
            set tab[h].real[1] = GetUnitX(u)
            // Stopped here :(
            set tab[h].real[2] = GetUnitY(u)
        endmethod

        method revive takes unit u returns boolean
            local integer h
            
            if filter(u) then
                if IsUnitType(u, UNIT_TYPE_DEAD) and GetUnitTypeId(u) != 0 then
                    set h = GetUnitUserData(u)
                    call SetUnitOwner(DUMMY, GetOwningPlayer(u), false)
                    call SetUnitX(DUMMY, tab[h][1])
                    call SetUnitY(DUMMY, tab[h][2])
                    call IssueImmediateOrderById(DUMMY, ORDER_ID)
                    return true
                endif
            endif
            return false
        endmethod
    endstruct
    
    private function onDeath takes nothing returns boolean
        local sr this
        local unit u = GetTriggerUnit()
        
        if filter(u) then
            call this.save(u)
        endif
        set u = null
        return false
    endfunction

    private function Ini takes nothing returns nothing
        call RegisterAnyUnitEvent(EVENT_PLAYER_UNIT_DEATH, function onDeath, null)
        set DUMMY = CreateUnit(Player(15), DUMMY_ID, 0.0, 0.0, 270.0)
        call UnitAddAbility(DUMMY, SPELL_ID)
    endfunction
    
    function ReviveUnit takes unit whichUnit returns boolean
        local sr this
        
        if this.revive(whichUnit) then
            return true
        endif
        return false
    endfunction

    function ReviveUnitAtPoint takes unit whichUnit, real x, real y, boolean flag returns boolean      
        if ReviveUnit(whichUnit) then
            if flag then
                call SetUnitPosition(whichUnit, x, y)
            else
                call SetUnitX(whichUnit, x)
                call SetUnitY(whichUnit, y)
            endif
            return true
        endif
        return false
    endfunction
endlibrary

I dont know why but this part
JASS:
        static method save takes unit u returns nothing
            local integer h
            local thistype this
            
            set h = GetUnitUserData(u)
            set tab[h].real[1] = GetUnitX(u)
            // Stopped here :(
            set tab[h].real[2] = GetUnitY(u)
        endmethod
wont save the real there D:
can someone show me what's I'm doing wrong there? thnks..
 
JASS:
struct Example

	private static Table TAB

	static method save takes unit u returns nothing
		local integer id = GetUnitUserData(u)
		set TAB[id].real[1] = GetUnitX(u)
		set TAB[id].real[2] = GetUnitY(u)	
	endmethod

	static method loadX takes unit u returns real
		return TAB[GetUnitUserData(u)].real[1]
	endmethod

	static method loadY takes unit u returns real
		return TAB[GetUnitUserData(u)].real[2]
	endmethod

	static method onInit takes nothing returns nothing
		set TAB = Table.create()
	endmethod
endstruct
 

Kazeon

Hosted Project: EC
Level 34
Joined
Oct 12, 2011
Messages
3,449
I always motivated to create something at it's best quality (**lls*it)

BPower suggested me to get rid of the hashtable and GetUnitHandle thingy in it's current version (I know it's good enough to be approved, but I'm a stupid perfectionist D: so I feel it's not decent enough)
so I have an idea to use your table and nesth's indexer, so while I'm finishing that system (which I think that system could be simply useful) I'm also learning how to code in vJass (it's my first vJass resource)

I see you have also not initialized the struct instance. You never use "this.create()". Perhaps you would like to start with more basic structs before doing something this advanced?
I have done it before, but I deleted it because I was too confused why the table won't save that shit D:
 
Level 19
Joined
Mar 18, 2012
Messages
1,716
Did you already realise that the correct x/y value is actually the place where the corpse rottens?
Storing x/y is not only unnecessary, but also may store outdated information. Imagine what ... the corpse get moved.

You don't neet a Table array, but only one Table.

RegisterAnyUnitEvent Where does this magical function come from?
 

Kazeon

Hosted Project: EC
Level 34
Joined
Oct 12, 2011
Messages
3,449
Where does this magical function come from?
I used someone's library (forgot the name) from JASS resources
Did you already realise that the correct x/y value is actually the place where the corpse rottens?
Storing x/y is not only unnecessary, but also may store outdated information. Imagine what ... the corpse get moved.
unnecessary? so what must I save there? :(
You don't neet a Table array, but only one Table.
sorry I haven't really understand how it works..
 
Dalvengyr said:
sorry I haven't really understand how it works..

Table uses a single hashtable with the instance as the parent key.

For example:

JASS:
globals
    Table data
endglobals

function Init takes nothing returns nothing
    set data = Table.create()
    set data[9001] = GetRandomInt(0, 99)

    // translates to
    set data = Table___allocate() // gets a unique integer
    call SaveInteger(Table___HASH, data, 9001, GetRandomInt(0, 99))
endfunction
You are only allowed 250 hashtables per map so Table helps avoid that, among other things.
 

Kazeon

Hosted Project: EC
Level 34
Joined
Oct 12, 2011
Messages
3,449
Table uses a single hashtable with the instance as the parent key.

For example:

JASS:
globals
    Table data
endglobals

function Init takes nothing returns nothing
    set data = Table.create()
    set data[9001] = GetRandomInt(0, 99)

    // translates to
    set data = Table___allocate() // gets a unique integer
    call SaveInteger(Table___HASH, data, 9001, GetRandomInt(0, 99))
endfunction
You are only allowed 250 hashtables per map so Table helps avoid that, among other things.

oh my, that's pretty nice info :grin: thnks! now I know how it works.. :D
 
Status
Not open for further replies.
Top