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

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
 
Level 31
Joined
Jul 10, 2007
Messages
6,306
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