GetRandomStruct

Waaaaagh

I lost all my rep and my title being a jerk
Reaction score
70
What is the best way to do this? e.g.:

JASS:
struct goodid extends pitemtype
    integer BUYid
    integer DISid
    
    static method create takes integer id, integer PUAid, integer BUYid, integer DISid, integer encumbrance returns goodid
        local goodid g=goodid.allocate()
        set g.id=id
        set g.PUAid=PUAid
        set g.BUYid=BUYid
        set g.DISid=DISid
        set g.encumbrance=encumbrance
        return g
    endmethod
endstruct


I want to randomly pick an index of goodid, but, I don't want to pick a null index. Should I just keep trying (random between 1 and 8190) until I get one? That works okay if you have 7104 of them, but if you have 2...

The best solution I can see is to keep track of the max index of goodid. How do I do that?
 

Ghan

Administrator - Servers are fun
Staff member
Reaction score
888
I shouldn't even be posting here, but....

What prevents you from sticking a global in the create method to track the number of structs you've created? (I assume something does since you most likely would have done that if it were possible.)

Could you do something similar to that where you actually call up the struct, but not have it directly inside the struct?
 

PurgeandFire

zxcvmkgdfg
Reaction score
509
I shouldn't even be posting here, but....

What prevents you from sticking a global in the create method to track the number of structs you've created? (I assume something does since you most likely would have done that if it were possible.)

Could you do something similar to that where you actually call up the struct, but not have it directly inside the struct?

Why would you use globals if you are using structs? :p Just make a struct variable to count. I didn't really read the question, I'm just basing my answer off of what you just said. xD
 

Ghan

Administrator - Servers are fun
Staff member
Reaction score
888
> Why would you use globals if you are using structs?

It sounded more like "count the structs." Why couldn't you use a global to do that? Just an integer.

I need to be educated out of my neanderthal ways. :p
 

Waaaaagh

I lost all my rep and my title being a jerk
Reaction score
70
What if I have 4000 goodids, and, suddenly, I destroy the first 2000?

If I add a global into the create method (and increment it by 1), then I would logically need to do the opposite in the onDestroy method. But, in the above scenario, I would have 2000 instances. However, they wouldn't be stored under 1-2000. They would be under 2001-4000. That's why I want the max index (4000), not the count (2000). Unless, of course, I am wrong with my thoughts, and vJass shifts everything down when you remove an instance.
 

Ghan

Administrator - Servers are fun
Staff member
Reaction score
888
> What if I have 4000 goodids, and, suddenly, I destroy the first 2000?

Depends on how you destroy them, I suppose.
Ideally, you would shift all the other structs down to fill in any holes and adjust your global to compensate.
 

Waaaaagh

I lost all my rep and my title being a jerk
Reaction score
70
And then, what happens to all of the handles pointing to your struct? They are now pointing to the struct ABOVE the one they should be...
 

Ghan

Administrator - Servers are fun
Staff member
Reaction score
888
> And then, what happens to all of the handles pointing to your struct?

You are determined to defeat me. :p
That's fine. I expected it, actually. :rolleyes:

Shift all of your handles down, too. Or re-create them....
 

Waaaaagh

I lost all my rep and my title being a jerk
Reaction score
70
Perhaps you would like to write me a system which keeps track of every reference I make to a struct, the handle I attach it to, and automatically shifts the references every time a struct is destroyed? Or, would you rather just keep track of the max index?

You may see why I chose the path I did.


Anyway, I think vJass does this anyway, and I just have to know which global it is (or maybe I am once again assuming too much).
 

Cohadar

master of fugue
Reaction score
209
Struct allocation system does NOT shift anything anywhere,
when you destroy a struct it leaves empty space there
XXXX0XXXXXXX0000000000
witch will be filled the next time you create some struct.

Why the hell do you need random structs anyways?
 

Waaaaagh

I lost all my rep and my title being a jerk
Reaction score
70
I was pretty sure that was how it worked.


I need random structs because I am using them to define types (like itemtypeid), and then I have structs which are objects, which you create using the type. Then, there is an item based shop, which sells these types, and is populated with all of the good types every x seconds. You can add staples to the shop, which will ALWAYS show up, and ban types from your store. Right now, I am struggling with only 2 things:

1) Getting a random struct
2) Defining inheritance

You see, I have this:

JASS:
    interface pitemtype
        integer id
        integer PUAid
        integer encumbrance
    endinterface
    
    struct pitemid extends pitemtype
        static method create takes integer id, integer PUAid, integer encumbrance returns pitemid
            local pitemid p=pitemid.allocate()
            set p.id=id
            set p.PUAid=PUAid
            set p.encumbrance=encumbrance
            return p
        endmethod
    endstruct

struct goodid extends pitemtype
    integer BUYid
    integer DISid
    
    static method create takes integer id, integer PUAid, integer BUYid, integer DISid, integer encumbrance returns goodid
        local goodid g=goodid.allocate()
        set g.id=id
        set g.PUAid=PUAid
        set g.BUYid=BUYid
        set g.DISid=DISid
        set g.encumbrance=encumbrance
        return g
    endmethod
endstruct

    struct equipid extends pitemtype
        integer dur
        integer durmax
        
        static method create takes integer id, integer PUAid, integer encumbrance, integer dur, integer durmax returns equipid
            local equipid e=equipid.allocate()
            set e.id=id
            set e.PUAid=PUAid
            set e.encumbrance=encumbrance
            set e.dur=dur
            set e.durmax=durmax
            return e
        endmethod
    endstruct


What I would like is for goodid to be either an equippable or a regular item. Do this, though, create would have to take a pitemtype, which I'm not sure I want to do, as that requires you to define something twice (once for a regular item, and once for the store). I might be ok with it, though, if I make goodid not extend pitemtype... Of course, it would be awesome if I had 2 full arrays for each instance of the shop, so I could define the maxstock and restocktime for each item UNIQUELY for each shop, even if they are selling the same item. It looks though, as if I will need to add those two parameters into the goodid, and make them constant for a single type. You can still simulate the effect by creating two goodid's with the same pitemtype sold, but with different maxstock and restock values, then ban one from a store, and the other from another store.
 

Cohadar

master of fugue
Reaction score
209
Store pitems in a collection,
then make GetRandomPitem function by using Iterator loop on collection

For a more details how look at GroupPickRandomUnit() implementation
 

Waaaaagh

I lost all my rep and my title being a jerk
Reaction score
70
And this solves my problem because collections DOES move structs down when you remove one, right?
 
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