Nestharus
o-o
- Reaction score
- 84
A variety of very helpful snippets. I came up with all of these on my own except for two. One of the two is a common modular conversion and the other is a common search =P.
I will continue to add to this list as I think up other good algorithms and what not ^_^.
Conversions
Searches
I will continue to add to this list as I think up other good algorithms and what not ^_^.
Conversions
JASS:
Functions for converting numbers into ids for arrays
----------------------------------------------------------------
//r is some arbitrary real number
//base is the base and should be under 1
//This is the most accurate way : )
private function Real2Id takes real r, real base returns integer
return R2I((r/base)-R2I((r/base)/8191)*8191)
endfunction
//If the real is less than or equal 1, it will return 1, etc.
private function Real2Id takes real r returns integer
return R2I(r-R2I(r/8191)*8191)
endfunction
//The most simple converter ever : ).
private function Integer2Id takes integer i returns integer
return i-R2I(i/8191)*8191
endfunction
----------------------------------------------------------------
//Nice conversion function from BS. It will convert what you put it into
//the specified base first and then it will round it up if the remainder is over 0 and then
//convert it back to standard base 10 giving you a number that fits perfectly : ). It's useful
//for building timers or going through a series of numbers that follow a pattern ^_^.
private function Convert takes real r, real base returns real
local real converted
local integer rounded
if r > base then
set converted = r/base
set rounded = R2I(converted)
return (R2I(converted-rounded+.999)+roundedTime)*base //this will round up if it's above the base
//or
return (R2I(converted-rounded)+roundedTime)*base //this will always round down if above the base
endif
return base
endfunction
Searches
JASS:
//This will either find a current variable or find a new one. Use id
//conversions to get the proper start id.
//This is meant to go through some sort of an array given a value to find.
//If it runs into 0, it knows that the value does not exist and returns
//a new id ^_^. Use a conversion function to get the id to start the
//search.
private function Find takes integer id, real find returns integer
loop
exitwhen var[id] == find or var[id] == 0
set id = id + 1
endloop
return id
endfunction
//The Super Dynamic Arrays <img src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" class="smilie smilie--sprite smilie--sprite8" alt=":D" title="Big Grin :D" loading="lazy" data-shortname=":D" />.. bit complicated : P
//Jesus4Lyf uses arrays similar to this one : ).
nextCode refers to the next code to go to
lastCode refers to the previous code to go to
currentCode refers to the currentCode and uses the array039;s id so it is always accessible
the functions to use are
NextCode- For looping
RemoveCode- for removing from an array
StreamCodeList- for adding to an array
So why is this array so dynamic? Well, let039;s say you have a single array and you want
a multi dimensional array in it. Well, one way is to make it static, meaning you have a
set number of positions etc like 50 positions in the first dimension and 50 slots per thingie ^_^.
Well, this will make the array fully dynamic meaning it could be anything from a 1 dimensional array to a
2 dimensional array with 40 dimensions and 20 slots per thingie. Very useful for when you039;re not sure what
you want to do or you want to leave it open.
private constant function CHECK_NEXT_CODE takes integer id returns boolean //checks to see if the next code exists
return (nextCode[codeStackLoop[id]] > 0)
endfunction
private function NextCode takes integer id returns integer //loops through the array from start to finish and back
local integer result = codeStackLoop[id]
if CHECK_NEXT_CODE(id) then
set codeStackLoop[id] = nextCode[codeStackLoop[id]]
elseif not CHECK_NEXT_CODE(id) then
set codeStackLoop[id] = firstCode[id]
endif
return codeStack[result]
endfunction
private function AddToCodeStackQueu takes integer stack returns nothing //adds to a queu for freed array indexes
set codeStackQueuIndex = codeStackQueuIndex + 1
set codeStackQueu[codeStackQueuIndex] = stack
endfunction
private function RemoveCodeFromStack takes integer id, integer stack returns integer //removes an index from the array
local integer recycled = currentCode[id]
if stack < currentCode[id] then
set codeStack[stack] = codeStack[currentCode[id]]
set codePointToStack[codeStack[stack]] = stack
endif
set nextCode[lastCode[currentCode[id]]] = 0
set lastCode[currentCode[id]] = 0
set currentCode[id] = currentCode[id] - 1
return recycled
endfunction
private function RemoveCode takes integer id, integer codeId returns nothing //removes code from the array
call AddToCodeStackQueu(RemoveCodeFromStack(id, codePointToStack[codeId]))
set codePointToStack[codeId] = 0
endfunction
private function CodeStackIndex takes integer id returns integer //attempts to get a recycled index
if codeStackQueuIndex > 0 then
set codeStackQueuIndex = codeStackQueuIndex - 1
return codeStackQueu[codeStackQueuIndex+1]
endif
return 0
endfunction
private function GetCodeStackIndex takes integer id returns integer //gets an index
local integer instancedIndex = CodeStackIndex(id)
if instancedIndex == 0 then
set codeStackIndex = codeStackIndex + 1
return codeStackIndex
endif
return instancedIndex
endfunction
private function NewCode takes integer id returns integer //creates new index
local integer instancedIndex = GetCodeStackIndex(id)
set lastCode[instancedIndex] = nextCode[lastCode[currentCode[id]]]
set nextCode[lastCode[instancedIndex]] = instancedIndex
set currentCode[id] = instancedIndex
return instancedIndex
endfunction
private function StreamCodeList takes integer codeId, integer id returns nothing //the main function that is called ^_^
local integer instancedIndex
if currentCode[id] > 0 then
set instancedIndex = NewCode(id)
else
set instancedIndex = GetCodeStackIndex(id)
set firstCode[id] = instancedIndex
set currentCode[id] = instancedIndex
set lastCode[currentCode[id]] = instancedIndex
set nextCode[currentCode[id]] = instancedIndex
set codeStackLoop[id] = firstCode[id]
endif
set codeStack[instancedIndex] = codeId
set codePointToStack[codeId] = instancedIndex
endfunction