| 05-10-2004, 04:13 AM | #1 |
Here is a Matrix function that I created for my project. Since my project isn't working as much as I hoped, I'm releasing this function... FEATURES: --Allows you store multiple integers in one variable --Stores up to 7452 integer variables in one variable --Allows for more multidimensional integer variable arrays --Allows more than 2 dimensions on the integer --Allows up to 92 integers with 81 array dimensions (or vice versa) --Lots others, but I can't think of them O.o LIMITATIONS: --No negative numbers. (Can be edited for it) --You require a wait 0.01 wait/sleep when calling up any of these functions 2 times in a row. --Bad visual of your variables (I suggest making an excel spreadsheet to counter) --Requires 3 Global Variables --Max Dimension is 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:
[color=green] Type Name Array[/color] String StoreMatrix Yes String MatrixDimensions Yes String MatrixValues Yes Requirements: Peppar's StringFragment function StringFragment function: 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 Code:
function CreateMatrix takes string Name, string Dims, string Vals returns nothing
local integer i = 0
local integer i2 = 0
local integer count = 0
local integer cmax = S2I(StringFragment(Dims, 0, "x") ) * S2I(StringFragment(Dims, 1, "x") )
loop
exitwhen i > 8192
if udg_StoreMatrix[i] == null then
set udg_StoreMatrix[i] = Name
set udg_MatrixDimensions[i] = Dims
set udg_MatrixValues[i] = Vals
loop
exitwhen count == cmax
if StringFragment(Vals, i2, "-") == "" then
set count = count + 1
else
set Vals = Vals + "-0"
set udg_MatrixValues[i] = Vals
set count = count + 1
endif
set i2 = i2 + 1
endloop
return
endif
set i = i + 1
endloop
endfunctionCheck Matrix ValueFunction Code:
function CheckValueMatrix takes string Name, string Row, string Col returns integer
local integer i = 0
local integer X = 0
local integer Value = 0
local integer COLS = S2I(Col)
local integer ROWS = S2I(Row)
loop
exitwhen i > 8192
if Name == udg_StoreMatrix[i] then
set X = S2I(StringFragment(udg_MatrixDimensions[i], 0, "x") )
set Value = S2I(StringFragment(udg_MatrixValues[i], (((COLS - 1) * X) + ROWS) - 1, "-") )
endif
set i = i + 1
endloop
return Value
endfunctionSet Matrix ValueFunction Code:
function SetValueMatrix takes string Name, string Row, string Col, string NewVal returns nothing
local integer i = 0
local string array tempstr
local integer i2 = 0
local integer i3 = 1
local integer COLS = S2I(Col)
local integer ROWS = S2I(Row)
local integer X = 0
local integer cmax
loop
exitwhen i > 8192
if Name == udg_StoreMatrix[i] then
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[(((COLS - 1) * X) + ROWS) - 1] = 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
endfunctionPlease put the functions in order as I have. Here is an example of using all 3 functions... Code:
function Trig_Untitled_Trigger_001_Actions 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 TriggerSleepAction( 0.01 )
call DisplayTextToPlayer(Player(0), 0, 0, I2S(CheckValueMatrix("Test", "2", "4") ) )
call SetValueMatrix("Test", "4", "4", "90")
call TriggerSleepAction( 0.01 )
call DisplayTextToPlayer(Player(0), 0, 0, I2S(CheckValueMatrix("Test", "4", "4") ) )
endfunctionPlease remember when your setting the values in the CreateMatrix() function, you set the row values in column 1 first, then the row values in column 2, etc... Have fun with it ^^ |
| 05-12-2004, 01:38 AM | #2 |
Another function that I find many uses for... Code:
function CombineNum takes integer First, integer PlusNum returns integer return S2I(I2S(First) + I2S(PlusNum) ) endfunction An example is. local integer x set x = CombineNum(1, 1) which basically sets x = 11 or: local integer x set x = CombineNum(35, 6) which sets x = 356 |
| 05-12-2004, 01:47 AM | #3 |
Code:
Nice, very nice! |
| 05-12-2004, 07:38 PM | #4 |
FIX: Fixed the needing of the 0.02 problem in the CheckValueMatrix. Here is the new code. Code:
function CheckValueMatrix takes string Name, string Row, string Col returns integer
local integer i = 0
local integer X = 0
local integer Value = 0
local boolean exitnow = false
local integer COLS = S2I(Col)
local integer ROWS = S2I(Row)
loop
exitwhen exitnow == true
if Name == udg_StoreMatrix[i] then
set X = S2I(StringFragment(udg_MatrixDimensions[i], 0, "x") )
set Value = S2I(StringFragment(udg_MatrixValues[i], (((COLS - 1) * X) + ROWS) - 1, "-") )
set exitnow = true
endif
set i = i + 1
endloop
return Value
endfunctionHere is SetValueMatrix's Fix. Code:
function SetValueMatrix takes string Name, string Row, string Col, string NewVal returns nothing
local integer i = 0
local string array tempstr
local integer i2 = 0
local integer i3 = 1
local integer COLS = S2I(Col)
local integer ROWS = S2I(Row)
local integer X = 0
local integer cmax
local boolean exitnow = false
loop
exitwhen exitnow == true
if Name == udg_StoreMatrix[i] then
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[(((COLS - 1) * X) + ROWS) - 1] = 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
set exitnow = true
endif
set i = i +1
endloop
endfunction |
| 10-14-2004, 02:54 AM | #5 |
Fixed some more minor bugs that disallowed you to use them constantly. (There are still some errors where you must use a wait 0.01/0.02!) Just got back into war3, will try to fix... I also shortened CombineNum to the minimum. |
