| 03-21-2006, 11:00 PM | #1 |
Here's a function I created a while back... I found some uses of it, and hope ya do too. LIMITATIONS: --Bad visual of your variables (I suggest making an excel spreadsheet to counter) --Requires 3 Global Variables --There's a limit on the dimensions (81x92) For those of you who don't know, a matrix is ... I dunno, here's a matrix example: Code: _ _ |4 4| |8 8| ¯ ¯ The above is a 2x2 matrix, very useful if you know how to use it. Here is the global variables you need: Code:
Code: [color=green] Type Name Array[/color] String StoreMatrix Yes String MatrixDimensions Yes String MatrixValues Yes Requirements: Peppar's StringFragment function Code:
Code:
function StringFragment takes string s, integer sectionNum, string cutPoints returns string
local integer n = sectionNum //wanted fragment
local integer i = 0 //character iterator
local integer w = 0 //fragment counter
local integer t //token iterator
local string u = "" //buffer
local string c //current character
local integer numTokens = 0 //token counter
local string array token //token array, for easier access later on
if s == "" or s == null then
return s
endif
if n < 0 then
return null
endif
loop
set c = SubString(cutPoints, i, i + 1)
if c == "" then
exitwhen true
else
set numTokens = numTokens + 1
set token[i] = c
endif
set i = i + 1
endloop
set i = 0
loop
set c = SubString(s, i, i + 1)
if c == "" then
if w == n then
return u
endif
return ""
endif
set t = 0
loop
if t >= numTokens then
set u = u + c
exitwhen true
elseif token[t] == c then
if u == "" then
exitwhen true
elseif w == n then
return u
else
set w = w + 1
set u = ""
exitwhen true
endif
endif
set t = t + 1
endloop
set i = i + 1
endloop
if w == n then
return u
endif
return ""
endfunctionCreate Matrix Function JASS:Code: function CreateMatrix takes string Name, string Dims, string Vals returns nothing local integer i = 0 local integer MatrixAmount = 8192 local integer count = 0 local integer cmax = S2I(StringFragment(Dims, 0, "x") ) * S2I(StringFragment(Dims, 1, "x") ) loop exitwhen i > MatrixAmount if udg_StoreMatrix[i] == null then set MatrixAmount = i + 1 set udg_StoreMatrix[i] = Name set udg_MatrixDimensions[i] = Dims set udg_MatrixValues[i] = Vals loop exitwhen count == cmax if StringFragment(Vals, count, "-") == "" then set count = count + 1 else set Vals = Vals + "-0" set udg_MatrixValues[i] = Vals set count = count + 1 endif endloop return endif set i = i + 1 endloop endfunction Check Matrix ValueFunction JASS:Code: function CheckValueMatrix takes string Name, integer Row, integer Col returns integer local integer i = 0 local integer MatrixAmount = 8192 local integer X = 0 local integer Value = 0 loop exitwhen i > MatrixAmount + 1 if Name == udg_StoreMatrix[i] then set MatrixAmount = i + 1 set X = S2I(StringFragment(udg_MatrixDimensions[i], 0, "x") ) set Value = S2I(StringFragment(udg_MatrixValues[i], (((Row - 1) * X) + Col) - 1, "-") ) endif set i = i + 1 endloop return Value endfunction Set Matrix ValueFunction JASS:Code: function SetValueMatrix takes string Name, integer Row, integer Col, integer NewVal returns nothing local integer i = 0 local integer MatrixAmount = 8192 local string array tempstr local integer i2 = 0 local integer i3 = 1 local integer X = 0 local integer cmax loop exitwhen i > MatrixAmount + 1 if Name == udg_StoreMatrix[i] then set MatrixAmount = i + 1 set cmax = S2I(StringFragment(udg_MatrixDimensions[i], 0, "x") ) * S2I(StringFragment(udg_MatrixDimensions[i], 1, "x") ) loop exitwhen i2 > cmax set tempstr[i2] = StringFragment(udg_MatrixValues[i], i2, "-") set i2 = i2 + 1 endloop set X = S2I(StringFragment(udg_MatrixDimensions[i], 0, "x") ) set tempstr[(((Col - 1) * X) + Row) - 1] = I2S(NewVal) set udg_MatrixValues[i] = tempstr[0] loop exitwhen i3 > cmax set udg_MatrixValues[i] = udg_MatrixValues[i] + "-" + tempstr[i3] set i3 = i3 + 1 endloop endif set i = i +1 endloop endfunction Please put the functions in order as I have. Here is an example of using all 3 functions... JASS:Code: function TestTrigger takes nothing returns nothing call CreateMatrix("Test", "4x4", "1-2-2-1-3-4-7-8-9-10-11-12-13-14-15") call DisplayTextToPlayer(Player(0), 0, 0, I2S(CheckValueMatrix("Test", 4, 4) ) ) call DisplayTextToPlayer(Player(0), 0, 0, I2S(CheckValueMatrix("Test", 2, 4) ) ) call SetValueMatrix("Test", 4, 4, 90) call DisplayTextToPlayer(Player(0), 0, 0, I2S(CheckValueMatrix("Test", 4, 4) ) ) endfunction |
| 03-22-2006, 05:02 PM | #2 |
And what does it do, exactly? I can't see how you couldn't just do this with an integer array. |
| 03-22-2006, 07:01 PM | #3 |
Allows for a large number of integers to be stored in one variable. |
| 03-22-2006, 07:04 PM | #4 |
That's called a variable array. |
| 03-22-2006, 07:24 PM | #5 |
we have a jass tag. And why would you use these instead of gamecache? |
| 04-05-2006, 02:27 AM | #6 | |
Fixed some errors: CheckValueMatrix will no longer check columns then rows. Fixed a bug where you would need a wait 0.01(or greater) line after creating another line of code using one of the Matrix functions in succession. Additional Note: When creating a Matrix with no initial values, make sure you don't leave the value entry to "". Make sure it's set to "0" else the function won't work. Quote:
Game Caches are only 3-dimensional. This goes a lot higher. |
| 04-05-2006, 04:38 AM | #7 |
Nonsense! Game caches are easily made infinite dimensional. Try "x-y-z-u-v-w" for your string, for example. |
| 04-05-2006, 04:57 AM | #8 | |
Quote:
Really? I've never used a game cache before, so I don't really know their uses. I guess I'll experiment with 'em... Well, I just looked at a game cache, and from my perspective, it looks as if it works like this: Code:
Stored Data <-- Sub-Category Sub-Category --> Stored Data
\ /
Category
/ \
Stored Data <-- Sub-Category Sub-Category --> Stored DataIs that correct? |
| 04-05-2006, 04:59 AM | #9 |
Yup. The game cache, properly abused, is simply badass. |
| 04-05-2006, 05:10 AM | #10 |
Hmmm, I'll take your word for it. I'll try to experiment with it. I've been writing down some way to encrypt player name, items, hero name, etc into something rather small (max of 40 chars) in order to save a character. Are there any tips on how I could use item caches to do this? Currently what I have thought up of requires 68 characters to be typed out which holds a maximum of 3844 different kinds of items in the map. This doesn't even include completed stuff like quests and which hero a unit has... Let me point out that the function I posted above can be used like this Code:
Stored Data --> Stored Data's Array --> Stored Data's Array's Array # = Player K = Kills D = Deaths L = Level LI = Lives Code:
# K D L LI 1(1x1) 16(1x2) 0 10 10 2(2x1) 7 2 10 8 3(3x1) 12 2 10 8 4(4x1) 9 5 10 5 5(5x1) 3 7 10 3 And all that info is stored in one variable. |
| 04-05-2006, 05:30 AM | #11 |
I wrote a bignum library for the purpose, but, to be honest, go with Vex's system. If there are constraints on your information that you want to try implementing into compression, do post. 3844 items is... nuts. However, if your unit can only hold 6 items, you can pack each into 3 base 32 chars without much trouble, for a total of only 18 for the items. I think you will see magnanimous improvement just from going to Vex's system. |
| 04-08-2006, 01:39 AM | #12 |
Thx, PipeDream, I'll check it out. Added another function for when you're testing your map to see if the correct variables are in place. JASS:function DrawMatrix takes string Name, integer who, integer Row, integer Col returns nothing local integer i = 0 local integer i2 = 1 local string array Holder local integer TL local string SL loop exitwhen i > Row call DisplayTextToPlayer(Player(who), 0, 0, "[" + Holder[i] + "]") set i = i + 1 set i2 = 1 loop exitwhen i2 > Col set TL = StringLength(I2S(CheckValueMatrix(Name, i, i2))) set SL = "" loop exitwhen TL == 8 set SL = SL + " " set TL = TL + 1 endloop if i2 == Col then set SL = "" endif set Holder[i] = Holder[i] + I2S(CheckValueMatrix(Name, i, i2)) + SL set i2 = i2 + 1 endloop endloop endfunction |
