HomeUser Control Panel (unavailable in archive)ForumsTutorialsArt GalleryResourcesMaps

Transforming a 7 digits integer into one from a range from 0 to 8192

12-12-2003, 02:08 PM#1
Vexorian
Since there are no way to get the target of a spell with the unit cast/starts/.../ a spell events. We are urged to use the issued order event to save the target into an array which is determined by the player number, unfortunnally that won't work when you want to have more than a hero / units with trigger enhanced spells, it is a whole mess when two units are ordered to use a spell in the same period.

So I came with the idea to use the return bug to convert the units into integer and use that stuff in the array, unfortunnally (And I will hate blizzard for this) the array limit is incredibly small (8192) and converted handles to integer take like 7 digits :...:

So I came with another Idea: to take those seven digits and use addition for they so each of those numbers will take a range from 0 to 70 then I added the player number before the addition

something like:

1156 = a unit owned by player 11 (12) wich addition of digits is 56
107 = a unit owned by player 1 (2) with addition of digits is 7

Well it works but there still is chance that two units will have the same number, and I only use 840 numbers of the 8192 possible so I want a way to convert a 7 digits number into a number that comes from 0 to 8191 so there we 'll have the smallest chances that two units are going to have the same number.
12-12-2003, 02:13 PM#2
KaTTaNa
Can't you just subtract a certain 7-digit constant so the first unit-index will result in 0? (The constant to subtract should be where the first unit handle is stored)
12-12-2003, 03:01 PM#3
Peppar
@KaTTaNa and Lord Vexorian: The first handle allocated has an integer representation of 1048576 or 0x100000. I second KaTTaNas advice that you should subtract this number from the handle integers.

In my map I used the User Data to enumerate units. A user data of 0 means that the unit hasn't been enumerated, and is assigned the value of a variable that is increased with every assignment. That way each of the units get an unique integer that works as an index to an array. The problem with this solution would be if you have lots and lots of units spawn, making the variable that keeps track of the highest index go above 8192 (You could limit this feature to heroes, though.)
12-12-2003, 03:11 PM#4
Vexorian
That's the main problem with substratic, if by any chance a unit takes a value higher than 8192 (after substratic) its spells won't work at all
12-12-2003, 08:26 PM#5
AIAndy
I suggest you store the target of the spell in an array at a position which you store in the user data. Or alternatively store the target in the user data directly using the return bug.
The last solution would be to use the heap and store the pointer to the struct where you store the target and any additional information in the user data.
12-13-2003, 08:21 PM#6
jmoritz
I strongly advise against using the return bug in any way possible. You never know when Blizzard may fix the bug, breaking all of your triggers. Even when they don't fix the bug, but alter the way War3 allocates memory, could break your trigger code. In other words: don't be so stupid as to use a memory pointer as an integer. From a programmer's point of view, it's just horrific.

You can easily use a unit's user data to store an index into an array. Now don't tell me your map spawns more than 8000 units that are all able to cast the same spell: I don't believe you!
12-13-2003, 09:18 PM#7
Vexorian
Quote:
Originally posted by AIAndy
I suggest you store the target of the spell in an array at a position which you store in the user data. Or alternatively store the target in the user data directly using the return bug.
The last solution would be to use the heap and store the pointer to the struct where you store the target and any additional information in the user data.


I thought about the heap, but with 1999999 possibilities the heap functions will need to use way a lot of arrays.

I came up with the solution of using the mod function, I don't think blizzard will ever fix the return bug, I don't think they will make major changes besides (if we are lucky) to fix the memory leaks. The return bug is really useful and I consider it the "return feature", so I will start a strike if they ever "fix" it -)
12-13-2003, 09:38 PM#8
AIAndy
How do you come to 1999999 possibilities?
Each unit can only have one order at a given time so per unit you need only one struct (1 slot for spells with a target unit and one for spells with a target point) to store the target information. That means you only need as much memory as you have units.
12-13-2003, 09:47 PM#9
Vexorian
I misunderstood you, I would use the heap but I don't like to mess with user data aka custom value since I use them for another system
12-13-2003, 10:17 PM#10
AIAndy
Using a struct on the heap here, you can still use the custom value for your previous usage (only one more indirection) and you can additionally use it for this.
12-17-2003, 06:06 PM#11
jmoritz
People are not listening to me :(

There are much better ways than using the pointer as an integer to fix your problem. Yes, it involves using custom value. But guess what? You can have your old system and your new system, BOTH use custom value. How? For example, like this:

Assign EVERY unit a unique custom value. Use a global integer to store the last value you used, and for the next unit, use a value or 1 higher. No, you are not going to get more than 8000 units. Trust me. If you do, there's still other ways to fix this.

That it? Yep. Just use this new custom value like you used the old value. You can use it for anything.

Fortunately, with 1.13, you won't be needing custom value as much as you used to.
12-17-2003, 06:52 PM#12
AIAndy
In a way that is a pointer too and you are doing the handling yourself.
While avoiding pointers in a high level language with structs and maybe classes is a good idea, in JASS you just don't have those and used in the right way you can make much nicer code (from a Programmers point of view) instead of using parallel arrays that cannot even be passed to functions causing lots of copy/paste.
12-17-2003, 07:52 PM#13
Vexorian
well, this whole thing is unnecesary now, since we have event responses for the targets of the spells