• 🏆 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!

[Lua][vJass] New Table

Cokemonkey11

Code Reviewer
Level 29
Joined
May 9, 2006
Messages
3,528
Cokemonkey11, this has been discussed already. You are like 2 years too late to be recommending changes to this resource. The original Table resources pales in comparison to this, anyway.

It was suggested to me by a user to make a project of mine work for both your Table and Vexorian's version since using yours with my resource returns syntax errors

Sounds like an API problem bro. Honestly I don't even know what to suggest to you. vJass doesn't support method overloading right?
 

Bribe

Code Moderator
Level 50
Joined
Sep 26, 2009
Messages
9,464
The two shouldn't be in the same map to begin with. The idea is for one hashtable to be able to be passed around, instead of several. Obviously 2 doesn't hurt, but a LITTLE bit of work on a mapmaker's part to make stuff work is to be expected. You can thank Nestharus that this resource has the name that it does now, as I originally called it NewTable.
 

Cokemonkey11

Code Reviewer
Level 29
Joined
May 9, 2006
Messages
3,528
but a LITTLE bit of work on a mapmaker's part to make stuff work is to be expected.

That's not the issue. There are countless resources that use Vexorian's Table. Not just new ones like mine but old ones that won't be updated as well. You made a huge mistake in thinking that your resource is strictly better than Vexorian's by assuming other resources would be updated to comply with your API. That's never acceptable.

You can thank Nestharus that this resource has the name that it does now, as I originally called it NewTable.

Wow, I would never have guessed that Nestharus would suggest something so ridiculous. \s

Table is an okay name. But only if your API is backwards compatible with Vexorian's (and TableBC doesn't cut it)

Like I said, I'm concerned that at this point it's too late. Without support for overloading methods, we'll never be able to fix this API issue. As long as we learn from the mistake though I suppose that's the best we can do.
 
Level 31
Joined
Jul 10, 2007
Messages
6,306
It's best to only use resources from one site. Each site has their own standards, and these are, for the most part, incompatible with each other.

wc3c has its own unit indexer
th has its own
thw also has its own

strictly speaking, I believe that you should use the stuff at THW since it's the newest/best, but to each their own.

If you try to mix resources from different sites together, it's not our problem if you run into problems.
 

Cokemonkey11

Code Reviewer
Level 29
Joined
May 9, 2006
Messages
3,528
It's best to only use resources from one site. Each site has their own standards, and these are, for the most part, incompatible with each other.

You're literally talking about things you essentially invented yourself. Hive doesn't have a "standard" set of libraries, and neither does wc3c. These are just resources that people have publicized for other's use.

wc3c has its own unit indexer
th has its own
thw also has its own

I don't even use a unit indexer. Just because each site happens to have one common indexer doesn't mean they're standardized.
 
Level 19
Joined
Mar 18, 2012
Messages
1,716
How do I remove the handle leak, when using TableArray. Afterall only struct Table has method remove
Also I don't want to flush or destroy it.
JASS:
local effect e = AddSpecialEffectTarget(this.effName, u, "origin")
set table[i].effect[this] = e
set e = null
//Code in between
call DestroyEffect(table[i].effect[this])
 
Level 19
Joined
Mar 18, 2012
Messages
1,716
I might be wrong, but that's the syntax for Table not for TableArray.

For the TableArray API no method remove exists, only flush() and destroy().

Maybe I have to use static Table array table instead of static TableArray table.

In my case the parent key saves user data of UnitIndexer, so I won't hit the OP limit for Table array.
 
Level 23
Joined
Apr 16, 2012
Messages
4,041
I wonder, if I do:

JASS:
function test takes nothing returns nothing
    local Table t = Table.create()
    set t.unit[0] = someunit
endfunction

how can I check if someunit is inside the Table? I think simple t.has(GetHandleId(someunit)) wont work, because I actually stored unit not an integer, but t.unit.has(...) wont compile

so I wonder if there is any elegant solution to this
 
Level 23
Joined
Apr 16, 2012
Messages
4,041
it is possible to have 12 player units yes, but to me more generic, I would like to make it with hundreds

what I did is stored under table.boolean[GetHandleId(unit)] = true and then check if it is true or not, just wanted to know if there is anything like table.unit.has but most likely not
 

Bannar

Code Reviewer
Level 26
Joined
Mar 19, 2008
Messages
3,140
I'll upload few libraries today or tomorrow and u will quickly realise the issue. As of now I fix this with additional boolean that user needs to provide, yet it's pain in the ass.
There are more to come too. Quick example: comparator for template containers. Comparator requires 2 values of TYPE and returns an integer. Let's say array in stored within table. There is no way to solve ".integer" case without additional code/variables.
 

Bannar

Code Reviewer
Level 26
Joined
Mar 19, 2008
Messages
3,140
Efficiency and maximum code reduction is what halved hive jass resources possibilities. Lot of ppl do not post anything because they ignore those "standards" and do not want others to smash thier work just because it doesnt follow some stupid code, don't constrain ourselves.

Ppl don't have to update their Table because of one freaking operator ;>
Not all of them will be using resources that require this "update". I'd be gratefull if you added this additional operator though. I'll add a notice within each resources of mine which takes advantage of this new "feature". Whatmore, someone who actually will be willing to use one of those resources is most likely to read the description and copy the Table update as well.
 

Bannar

Code Reviewer
Level 26
Joined
Mar 19, 2008
Messages
3,140
One problem though: new update lacks: implement integerm thus functionality isn't there yet ^^


One more thing: your demo code has a mistake: call a.unit.remove('ABCD') does not compile what shouldn't really surprise us since only basic structs, handles and main Table struct contains "remove" member. The rest relies on intuitive operator which returns table id which then performs inner remove operation. Thus removing unit requires us to write: a.handle.remove('ABCD') instead.
- yes I know that this is probably due to multiple updates done to the Table while demos stayed as there were as the very begining :D

Here I updated the demo and added struct what enables demo to be copy-pasted and tested within map:
JASS:
struct table_demo extends array
    private static method demo takes nothing returns nothing
        //Create it:
        local Table a = Table.create()
         
        //Use it:
        local boolean b = a.has(69)
        set a[654321] = 'A'
        set a[54321] = 'B'
        set a.unit[12345] = GetTriggerUnit()
        set a.unit[GetHandleId(a.unit[12345])] = GetSpellTargetUnit()
        set a.real['ABCD'] = 3.14159
        set a.integer[133] = 21

        //remove entries
        call a.handle.remove('ABCD')
        call a.remove(54321)

        //Flush/destroy it:
        call a.destroy()

        //Or, only flush it:
        call a.flush()
    endmethod
endstruct
 
Last edited:
Level 31
Joined
Jul 10, 2007
Messages
6,306
Complete API

unit.remove
unit.has

etc

Ran into a wall because you can't save null values into a hashtable. Using macros, so need remove to be on all types : |.

JASS:
library Table /* made by Bribe, special thanks to Vexorian & Nestharus, version 3.1.0.3
    
    One map, one hashtable. Welcome to NewTable 3.2
    
    This library was originally called NewTable so it didn't conflict with
    the API of Table by Vexorian. However, the damage is done and it's too
    late to change the library name now. To help with damage control, I
    have provided an extension library called TableBC, which bridges all
    the functionality of Vexorian's Table except for 2-D string arrays &
    the ".flush(integer)" method. I use ".flush()" to flush a child hash-
    table, because I wanted the API in NewTable to reflect the API of real
    hashtables (I thought this would be more intuitive).
    
    API
    
    ------------
    struct Table
    | static method create takes nothing returns Table
    |     create a new Table
    |    
    | method destroy takes nothing returns nothing
    |     destroy it
    |    
    | method flush takes nothing returns nothing
    |     flush all stored values inside of it
    |    
    | method remove takes integer key returns nothing
    |     remove the value at index "key"
    |    
    | method operator []= takes integer key, $TYPE$ value returns nothing
    |     assign "value" to index "key"
    |    
    | method operator [] takes integer key returns $TYPE$
    |     load the value at index "key"
    |    
    | method has takes integer key returns boolean
    |     whether or not the key was assigned
    |
    ----------------
    struct TableArray
    | static method operator [] takes integer array_size returns TableArray
    |     create a new array of Tables of size "array_size"
    |
    | method destroy takes nothing returns nothing
    |     destroy it
    |
    | method flush takes nothing returns nothing
    |     flush and destroy it
    |
    | method operator size takes nothing returns integer
    |     returns the size of the TableArray
    |
    | method operator [] takes integer key returns Table
    |     returns a Table accessible exclusively to index "key"
*/
    
globals
    private integer less = 0    //Index generation for TableArrays (below 0).
    private integer more = 8190 //Index generation for Tables.
    //Configure it if you use more than 8190 "key" variables in your map (this will never happen though).
    
    private hashtable ht = InitHashtable()
    private key sizeK
    private key listK
endglobals
    
private struct dex extends array
    static method operator size takes nothing returns Table
        return sizeK
    endmethod
    static method operator list takes nothing returns Table
        return listK
    endmethod
endstruct
    
private struct handles extends array
    method has takes integer key returns boolean
        return HaveSavedHandle(ht, this, key)
    endmethod
    method remove takes integer key returns nothing
        call RemoveSavedHandle(ht, this, key)
    endmethod
endstruct
    
private struct agents extends array
    method operator []= takes integer key, agent value returns nothing
        call SaveAgentHandle(ht, this, key, value)
    endmethod
endstruct
    
//! textmacro NEW_ARRAY takes SUPER, FUNC, TYPE
private struct $TYPE$s extends array
    method operator [] takes integer key returns $TYPE$
        return Load$FUNC$(ht, this, key)
    endmethod
    method operator []= takes integer key, $TYPE$ value returns nothing
        call Save$FUNC$(ht, this, key, value)
    endmethod
    method has takes integer key returns boolean
        return HaveSaved$SUPER$(ht, this, key)
    endmethod
    method remove takes integer key returns nothing
        call RemoveSaved$SUPER$(ht, this, key)
    endmethod
endstruct
private module $TYPE$m
    method operator $TYPE$ takes nothing returns $TYPE$s
        return this
    endmethod
endmodule
//! endtextmacro
    
//Run these textmacros to include the entire hashtable API as wrappers.
//Don't be intimidated by the number of macros - Vexorian's map optimizer is
//supposed to kill functions which inline (all of these functions inline).
//! runtextmacro NEW_ARRAY("Real", "Real", "real")
//! runtextmacro NEW_ARRAY("Boolean", "Boolean", "boolean")
//! runtextmacro NEW_ARRAY("String", "Str", "string")
//New textmacro to allow table.integer[] syntax for compatibility with textmacros that might desire it.
//! runtextmacro NEW_ARRAY("Integer", "Integer", "integer")
    
//! runtextmacro NEW_ARRAY("Handle", "PlayerHandle", "player")
//! runtextmacro NEW_ARRAY("Handle", "WidgetHandle", "widget")
//! runtextmacro NEW_ARRAY("Handle", "DestructableHandle", "destructable")
//! runtextmacro NEW_ARRAY("Handle", "ItemHandle", "item")
//! runtextmacro NEW_ARRAY("Handle", "UnitHandle", "unit")
//! runtextmacro NEW_ARRAY("Handle", "AbilityHandle", "ability")
//! runtextmacro NEW_ARRAY("Handle", "TimerHandle", "timer")
//! runtextmacro NEW_ARRAY("Handle", "TriggerHandle", "trigger")
//! runtextmacro NEW_ARRAY("Handle", "TriggerConditionHandle", "triggercondition")
//! runtextmacro NEW_ARRAY("Handle", "TriggerActionHandle", "triggeraction")
//! runtextmacro NEW_ARRAY("Handle", "TriggerEventHandle", "event")
//! runtextmacro NEW_ARRAY("Handle", "ForceHandle", "force")
//! runtextmacro NEW_ARRAY("Handle", "GroupHandle", "group")
//! runtextmacro NEW_ARRAY("Handle", "LocationHandle", "location")
//! runtextmacro NEW_ARRAY("Handle", "RectHandle", "rect")
//! runtextmacro NEW_ARRAY("Handle", "BooleanExprHandle", "boolexpr")
//! runtextmacro NEW_ARRAY("Handle", "SoundHandle", "sound")
//! runtextmacro NEW_ARRAY("Handle", "EffectHandle", "effect")
//! runtextmacro NEW_ARRAY("Handle", "UnitPoolHandle", "unitpool")
//! runtextmacro NEW_ARRAY("Handle", "ItemPoolHandle", "itempool")
//! runtextmacro NEW_ARRAY("Handle", "QuestHandle", "quest")
//! runtextmacro NEW_ARRAY("Handle", "QuestItemHandle", "questitem")
//! runtextmacro NEW_ARRAY("Handle", "DefeatConditionHandle", "defeatcondition")
//! runtextmacro NEW_ARRAY("Handle", "TimerDialogHandle", "timerdialog")
//! runtextmacro NEW_ARRAY("Handle", "LeaderboardHandle", "leaderboard")
//! runtextmacro NEW_ARRAY("Handle", "MultiboardHandle", "multiboard")
//! runtextmacro NEW_ARRAY("Handle", "MultiboardItemHandle", "multiboarditem")
//! runtextmacro NEW_ARRAY("Handle", "TrackableHandle", "trackable")
//! runtextmacro NEW_ARRAY("Handle", "DialogHandle", "dialog")
//! runtextmacro NEW_ARRAY("Handle", "ButtonHandle", "button")
//! runtextmacro NEW_ARRAY("Handle", "TextTagHandle", "texttag")
//! runtextmacro NEW_ARRAY("Handle", "LightningHandle", "lightning")
//! runtextmacro NEW_ARRAY("Handle", "ImageHandle", "image")
//! runtextmacro NEW_ARRAY("Handle", "UbersplatHandle", "ubersplat")
//! runtextmacro NEW_ARRAY("Handle", "RegionHandle", "region")
//! runtextmacro NEW_ARRAY("Handle", "FogStateHandle", "fogstate")
//! runtextmacro NEW_ARRAY("Handle", "FogModifierHandle", "fogmodifier")
//! runtextmacro NEW_ARRAY("Handle", "HashtableHandle", "hashtable")
    
struct Table extends array
    
    // Implement modules for intuitive syntax (tb.handle; tb.unit; etc.)
    implement realm
    implement integerm
    implement booleanm
    implement stringm
    implement playerm
    implement widgetm
    implement destructablem
    implement itemm
    implement unitm
    implement abilitym
    implement timerm
    implement triggerm
    implement triggerconditionm
    implement triggeractionm
    implement eventm
    implement forcem
    implement groupm
    implement locationm
    implement rectm
    implement boolexprm
    implement soundm
    implement effectm
    implement unitpoolm
    implement itempoolm
    implement questm
    implement questitemm
    implement defeatconditionm
    implement timerdialogm
    implement leaderboardm
    implement multiboardm
    implement multiboarditemm
    implement trackablem
    implement dialogm
    implement buttonm
    implement texttagm
    implement lightningm
    implement imagem
    implement ubersplatm
    implement regionm
    implement fogstatem
    implement fogmodifierm
    implement hashtablem
    
    method operator handle takes nothing returns handles
        return this
    endmethod
    
    method operator agent takes nothing returns agents
        return this
    endmethod
    
    //set this = tb[GetSpellAbilityId()]
    method operator [] takes integer key returns Table
        return LoadInteger(ht, this, key) //return this.integer[key]
    endmethod
    
    //set tb[389034] = 8192
    method operator []= takes integer key, Table tb returns nothing
        call SaveInteger(ht, this, key, tb) //set this.integer[key] = tb
    endmethod
    
    //set b = tb.has(2493223)
    method has takes integer key returns boolean
        return HaveSavedInteger(ht, this, key) //return this.integer.has(key)
    endmethod
    
    //call tb.remove(294080)
    method remove takes integer key returns nothing
        call RemoveSavedInteger(ht, this, key) //call this.integer.remove(key)
    endmethod
    
    //Remove all data from a Table instance
    method flush takes nothing returns nothing
        call FlushChildHashtable(ht, this)
    endmethod
    
    //local Table tb = Table.create()
    static method create takes nothing returns Table
        local Table this = dex.list[0]
        
        if this == 0 then
            set this = more + 1
            set more = this
        else
            set dex.list[0] = dex.list[this]
            call dex.list.remove(this) //Clear hashed memory
        endif
        
        debug set dex.list[this] = -1
        return this
    endmethod
    
    // Removes all data from a Table instance and recycles its index.
    //
    //     call tb.destroy()
    //
    method destroy takes nothing returns nothing
        debug if dex.list[this] != -1 then
            debug call BJDebugMsg("Table Error: Tried to double-free instance: " + I2S(this))
            debug return
        debug endif
        
        call this.flush()
        
        set dex.list[this] = dex.list[0]
        set dex.list[0] = this
    endmethod
    
    //! runtextmacro optional TABLE_BC_METHODS()
endstruct
    
//! runtextmacro optional TABLE_BC_STRUCTS()
    
struct TableArray extends array
    
    //Returns a new TableArray to do your bidding. Simply use:
    //
    //    local TableArray ta = TableArray[array_size]
    //
    static method operator [] takes integer array_size returns TableArray
        local Table tb = dex.size[array_size] //Get the unique recycle list for this array size
        local TableArray this = tb[0]         //The last-destroyed TableArray that had this array size
        
        debug if array_size <= 0 then
            debug call BJDebugMsg("TypeError: Invalid specified TableArray size: " + I2S(array_size))
            debug return 0
        debug endif
        
        if this == 0 then
            set this = less - array_size
            set less = this
        else
            set tb[0] = tb[this]  //Set the last destroyed to the last-last destroyed
            call tb.remove(this)  //Clear hashed memory
        endif
        
        set dex.size[this] = array_size //This remembers the array size
        return this
    endmethod
    
    //Returns the size of the TableArray
    method operator size takes nothing returns integer
        return dex.size[this]
    endmethod
    
    //This magic method enables two-dimensional[array][syntax] for Tables,
    //similar to the two-dimensional utility provided by hashtables them-
    //selves.
    //
    //ta[integer a].unit[integer b] = unit u
    //ta[integer a][integer c] = integer d
    //
    //Inline-friendly when not running in debug mode
    //
    method operator [] takes integer key returns Table
        static if DEBUG_MODE then
            local integer i = this.size
            if i == 0 then
                call BJDebugMsg("IndexError: Tried to get key from invalid TableArray instance: " + I2S(this))
                return 0
            elseif key < 0 or key >= i then
                call BJDebugMsg("IndexError: Tried to get key [" + I2S(key) + "] from outside TableArray bounds: " + I2S(i))
                return 0
            endif
        endif
        return this + key
    endmethod
    
    //Destroys a TableArray without flushing it; I assume you call .flush()
    //if you want it flushed too. This is a public method so that you don't
    //have to loop through all TableArray indices to flush them if you don't
    //need to (ie. if you were flushing all child-keys as you used them).
    //
    method destroy takes nothing returns nothing
        local Table tb = dex.size[this.size]
        
        debug if this.size == 0 then
            debug call BJDebugMsg("TypeError: Tried to destroy an invalid TableArray: " + I2S(this))
            debug return
        debug endif
        
        if tb == 0 then
            //Create a Table to index recycled instances with their array size
            set tb = Table.create()
            set dex.size[this.size] = tb
        endif
        
        call dex.size.remove(this) //Clear the array size from hash memory
        
        set tb[this] = tb[0]
        set tb[0] = this
    endmethod
    
    private static Table tempTable
    private static integer tempEnd
    
    //Avoids hitting the op limit
    private static method clean takes nothing returns nothing
        local Table tb = .tempTable
        local integer end = tb + 0x1000
        if end < .tempEnd then
            set .tempTable = end
            call ForForce(bj_FORCE_PLAYER[0], function thistype.clean)
        else
            set end = .tempEnd
        endif
        loop
            call tb.flush()
            set tb = tb + 1
            exitwhen tb == end
        endloop
    endmethod
    
    //Flushes the TableArray and also destroys it. Doesn't get any more
    //similar to the FlushParentHashtable native than this.
    //
    method flush takes nothing returns nothing
        debug if this.size == 0 then
            debug call BJDebugMsg("TypeError: Tried to flush an invalid TableArray instance: " + I2S(this))
            debug return
        debug endif
        set .tempTable = this
        set .tempEnd = this + this.size
        call ForForce(bj_FORCE_PLAYER[0], function thistype.clean)
        call this.destroy()
    endmethod
    
endstruct
    
endlibrary

Here is the resource in question
JASS:
library TableField uses Table, Init
    //! textmacro CREATE_TABLE_FIELD takes ACCESS_MODIFIER, TYPE, NAME, RETURN_TYPE
        private static Table t$NAME$
            
        $ACCESS_MODIFIER$ method operator $NAME$ takes nothing returns $RETURN_TYPE$
            return t$NAME$.$TYPE$[this]
        endmethod
        $ACCESS_MODIFIER$ method operator $NAME$= takes $RETURN_TYPE$ value returns nothing
            set t$NAME$.$TYPE$[this] = value
        endmethod
        $ACCESS_MODIFIER$ method $NAME$_clear takes nothing returns nothing
            call t$NAME$.$TYPE$.remove(this)
        endmethod
    //! endtextmacro
    
    //! textmacro CREATE_TABLE_FIELD_ARRAY takes TYPE, NAME, RETURN_TYPE
        private struct T$NAME$ extends array
            private static Table table
            
            method operator [] takes integer index returns $RETURN_TYPE$
                return table.$TYPE$[index]
            endmethod
            method operator []= takes integer index, $RETURN_TYPE$ value returns nothing
                set table.$TYPE$[index] = value
            endmethod
            static method remove takes integer index returns nothing
                call table.$TYPE$.remove(index)
            endmethod
            
            private static method init takes nothing returns nothing
                set table = Table.create()
            endmethod
            
            implement Init
        endstruct
    //! endtextmacro
    
    //! textmacro USE_TABLE_FIELD_ARRAY takes ACCESS_MODIFIER, NAME
        $ACCESS_MODIFIER$ static T$NAME$ $NAME$ = 0
    //! endtextmacro
    
    //! textmacro INITIALIZE_TABLE_FIELD takes NAME
        set t$NAME$ = Table.create()
    //! endtextmacro
endlibrary
 
Level 10
Joined
Sep 19, 2011
Messages
527
hey bribe, why don't you just do something like this (i'm asking because i didn't really read all the code, scares a little bit xD):

JASS:
    //! textmacro Register takes NAME, TYPE, LOAD_FUNCTION, SAVE_FUNCTION, HAVE_FUNCTION, REMOVE_FUNCTION
    private struct $NAME$ extends array
        method operator [] takes integer index returns $TYPE$
            return $LOAD_FUNCTION$(hashTable, this, index + Table(this).column * Table(this).size)
        endmethod
        method operator []= takes integer index, $TYPE$ value returns nothing
            call $SAVE_FUNCTION$(hashTable, this, index + Table(this).column * Table(this).size, value)
        endmethod
        method has takes integer index returns boolean
            return $HAVE_FUNCTION$(hashTable, this, index + Table(this).column * Table(this).size)
        endmethod
        method remove takes integer index returns nothing
            call $REMOVE_FUNCTION$(hashTable, this, index + Table(this).column * Table(this).size)
        endmethod
    endstruct
    private module $NAME$Module
        method operator $TYPE$ takes nothing returns $NAME$
            return this
        endmethod
    endmodule
    //! endtextmacro
    
    //! runtextmacro Register("Integer", "integer", "LoadInteger", "SaveInteger", "HaveSavedInteger", "RemoveSavedInteger")
    //  runtextmacro ...
    
    struct Table
        implement IntegerModule
        // implement ...
        
        readonly integer column = 0
        integer size = 0
        
        method operator [] takes integer column returns thistype
            set this.column = column
            return this
        endmethod
        
        method flush takes nothing returns nothing
            call FlushChildHashtable(hashTable, this)
        endmethod
        
        method destroy takes nothing returns nothing
            call this.flush()
            call this.deallocate()
        endmethod
    endstruct
it would be:

JASS:
local Table tableArray = Table.create()

// dinamic size
set tableArray.size = n

// set
tableArray[x].integer[z] = value
// get
tableArray[x].integer[z]
// has
tableArray[x].integer.has(z)
// an so on...
for normal tables it would be the same.
 

Bribe

Code Moderator
Level 50
Joined
Sep 26, 2009
Messages
9,464
Post all the code that you have that reads/writes from the TableArray. I will take a look at it for you and tell you where the problem is. Both Table and TableArray are extremely thoroughly tested, but it's hard to find bugs in your own code when you want to find the fault with something else.
 
Level 29
Joined
Oct 24, 2012
Messages
6,543
Here is the code bribe.

JASS:
        // Saving.
        method addItem takes integer itemId, integer weight returns nothing
            set this.index = this.index + 1
            set this.ttlWeight = this.ttlWeight + weight
            set itemIdTable[this][index] = itemId
            set itemDropTable[this][itemId] = this.ttlWeight
        endmethod

JASS:
        // Loading.
        private method dropItem takes real x, real y returns boolean
            local integer r
            local integer L = 1
            local integer L1 = 1
            local integer end = 0
            local integer array weights
            set weights[0] = 0
            if this != 0 then
                loop
                    exitwhen L > this.index
                    set weights[L] = itemDropTable[this][itemIdTable[this][L]]
                    set end = end + 1
                    set L = L + 1
                endloop
                set L = 1
                loop
                    exitwhen L > this.multiDrop
                        set r = GetRandomInt( 1, this.ttlWeight)
                        loop
                            exitwhen L1 > end
                            if r > weights[L1 - 1] and r < weights[L1] then
                                call CreateItem( itemIdTable[this][L1], x, y)
                            endif
                            set L1 = L1 + 1
                        endloop
                    set L = L + 1
                endloop
            endif
            return this.dropTypeTableAlso
        endmethod

Thanks for looking at this.
 

Bribe

Code Moderator
Level 50
Joined
Sep 26, 2009
Messages
9,464
Hmm, your code is more complex than I thought. I have a hard time telling what's a TableArray and what's a Table storing values. Can you comment the part that's causing a problem, and also list the relevant global declarations? It might help if you post the whole code if it's really this complex, though. There might be another area that's causing a conflict.
 
Level 29
Joined
Oct 24, 2012
Messages
6,543
Hmm, your code is more complex than I thought. I have a hard time telling what's a TableArray and what's a Table storing values. Can you comment the part that's causing a problem, and also list the relevant global declarations? It might help if you post the whole code if it's really this complex, though. There might be another area that's causing a conflict.

Sorry I forgot the global declarations.

JASS:
private Table itemIdTable = TableArray[0x2000]
private Table itemDropTable = TableArray[0x2000]

The above code is what stores and loads. There is one other thing that stores but it does it in the same way.
 
Last edited:
Top