• Listen to a special audio message from Bill Roper to the Hive Workshop community (Bill is a former Vice President of Blizzard Entertainment, Producer, Designer, Musician, Voice Actor) 🔗Click here to hear his message!
  • Read Evilhog's interview with Gregory Alper, the original composer of the music for WarCraft: Orcs & Humans 🔗Click here to read the full interview.

How to integrate Bribe's Table

Status
Not open for further replies.
Level 8
Joined
Feb 3, 2013
Messages
277
hellluuuu, below is a simple item drop system, can someone explain to me how I can integrate Table into this system? I am still not sure how Table works... even after reading Bribe's demo - furthermore with this system.

Also a side question -
can someone explain what binary trees are good for with a wc3 example?

JASS:
library ItemDropList requires RegisterPlayerUnitEvent, SharedList

    private struct DropList extends array
        implement SharedList
        
        integer itemId
        integer itemChance
        
        static thistype nn
    endstruct
    
    struct IDL extends array
        implement SharedList
        
        integer unitId
        integer initChance
        DropList iList
        
        static thistype l
        static thistype nn
        
        method addItem takes integer itemId, integer itemChance returns nothing
            local DropList tList = .iList.enqueue()
            
            set tList.itemId = itemId
            set tList.itemChance = itemChance
        endmethod
        
        static method createDropList takes integer unitTypeId, integer chanceToDrop returns thistype
            local thistype this = l.enqueue()
            
            set .unitId = unitTypeId
            set .initChance = chanceToDrop
            set .iList = DropList.create()
            
            return this
        endmethod
        
        static method checkForDrops takes nothing returns nothing
            local thistype this = l.first
            local unit du       = GetTriggerUnit()
            local DropList tList
            
            loop
                exitwhen this == l.sentinel
                set nn = .next
                
                //
                if GetUnitTypeId(du) == .unitId and GetRandomInt(1, 100) <= .initChance then
                    set tList = .iList.first
                    loop
                        exitwhen tList == .iList.sentinel
                        set tList.nn = tList.next
                        if GetRandomInt(1, 100) <= tList.itemChance then
                            call CreateItem(tList.itemId, GetUnitX(du), GetUnitY(du))
                        endif
                        set tList = tList.nn
                    endloop
                endif
                //
                
                set this = nn
            endloop
            
            set du = null
        endmethod
        
        static method onInit takes nothing returns nothing
            set l = thistype.create()
            
            call RegisterPlayerUnitEvent(EVENT_PLAYER_UNIT_DEATH, function thistype.checkForDrops)
        endmethod
    endstruct
    
endlibrary
 
You are going to add another level of abstraction.

Rather than 1 static IDL, you will have one IDL for every unit type id. This IDL will be stored inside of a static table.

onCreateForUnitTypeId (the thing does not currently exist, use table.has(unitTypeId))

set myDropLists = IDL.create()
set table[unitTYpeId] myDropLists

Now, when you add a new dropList, you will add it to the table

return thistype(dropTable[unitTypeId]).enqueue()

The rest works as normal, except that you won't need this anymore

if GetUnitTypeId(du) == .unitId and GetRandomInt(1, 100) <= .initChance then

You will look up the drop lists for the unit type id

set dropLists = thistype(table[unitTypeId]).first

And then you'll iterate over it. You can check drop chance for the list and so on =).


Also, you should probably be using a Queue.

http://www.hiveworkshop.com/forums/jass-resources-412/snippet-queue-234003/

The Shared thing is really only for people who aren't sure how to use collections =). I should probably hide it in the various versions and enable it when debug mode is enabled ; P.

edit
JASS:
static method createDropList takes integer unitTypeId, integer chanceToDrop returns thistype
	local thistype this

	if (not table.has(unitTypeId)) then
		set this = create()

		set table[unitTypeId] = this

		set this = enqueue()
	endif
            
	set .unitId = unitTypeId
	set .initChance = chanceToDrop
	set .iList = DropList.create()
            
	return this
endmethod

JASS:
static method checkForDrops takes nothing returns nothing
	local unit du       = GetTriggerUnit()
	local thistype this = thistype(table[GetUnitTypeId(du))).first
	local DropList tList

	//the next protection is only useful if you plan on removing elements
	//during iteration, which you aren't doing, so no point
	loop
		exitwhen this == l.sentinel

		if GetRandomInt(1, 100) <= .initChance then
			set tList = .iList.first
			loop
				exitwhen tList == .iList.sentinel

				if GetRandomInt(1, 100) <= tList.itemChance then
					call CreateItem(tList.itemId, GetUnitX(du), GetUnitY(du))
 				endif

				set tList = tList.next
			endloop
		endif

		set this = this.next
	endloop
            
	set du = null
endmethod

edit
Actually, you could just use a Stack

In order from least complex to most complex

Stack
Queue
List

so try to use the least complex data structure that you can use ;p

edit
can someone explain what binary trees are good for with a wc3 example?

Here is one example of use

http://www.hiveworkshop.com/forums/...talog-level-slot-group-version-filter-212758/

It's a filter over a Catalog that accounts for level ranges, slots, and groups. Level ranges require AVL Tree, which is a form of a balanced BST (binary search tree).

The level is searched for within the tree. A tree continues to split. If you search in a list, you have to possibly go over all of the elements O(n). If you search in a tree, it's only O(Log n).
 
Status
Not open for further replies.
Top