cubescript multidimensional arrays (WIP)
git-svn-id: svn://svn.code.sf.net/p/actiongame/code/trunk/ac@6816 e016b1c5-ed68-44aa-a2f7-31cac88cffde
This commit is contained in:
@@ -0,0 +1,204 @@
|
||||
// Cubescript Multidimensional Arrays
|
||||
|
||||
islists = [
|
||||
bool = 0
|
||||
loop i (listlen $arg1) [
|
||||
if (> (listlen (at $arg1 $i)) 1) [
|
||||
bool = 1; break
|
||||
]
|
||||
]
|
||||
result $bool
|
||||
]
|
||||
|
||||
hasQuotePair = [
|
||||
bool = 0
|
||||
got_open_quote = -1
|
||||
got_close_quote = -1
|
||||
loop r (strlen $arg1) [
|
||||
if (testchar (substr $arg1 $r 1) 8) [
|
||||
if (= $got_open_quote -1) [
|
||||
got_open_quote = $r
|
||||
] [
|
||||
got_close_quote = $r
|
||||
break;
|
||||
]
|
||||
]
|
||||
]
|
||||
if (&& (!= $got_open_quote -1) (&& (!= $got_close_quote -1) (!= $got_open_quote $got_close_quote))) [
|
||||
result 1
|
||||
] [ result 0 ]
|
||||
]
|
||||
|
||||
reverseString = [
|
||||
oldstr = $arg1
|
||||
newstr = ""
|
||||
|
||||
loop i (strlen $oldstr) [
|
||||
newstr = (concatword $newstr (substr $oldstr (* (+ $i 1) -1) 1))
|
||||
]
|
||||
result $newstr
|
||||
]
|
||||
|
||||
extractSubarrayName = [
|
||||
open_bracket_pos = $arg2
|
||||
subarray_name = ""
|
||||
|
||||
loop d (strlen $arg1) [
|
||||
tmpchar = (substr $arg1 (- $open_bracket_pos (+ $d 2)) 1)
|
||||
if (testchar $tmpchar 7) break [
|
||||
if (&& (! (strcmp $tmpchar "[")) (! (strcmp $tmpchar "]"))) [
|
||||
subarray_name = (concatword $subarray_name $tmpchar)
|
||||
]
|
||||
]
|
||||
]
|
||||
result (reverseString $subarray_name)
|
||||
]
|
||||
|
||||
MDArray_New_Subarray = [
|
||||
if (= (findlist (getalias $arg1) $arg2) -1) [
|
||||
add2list $arg1 $arg2
|
||||
if (islists $arg3) [
|
||||
add2list $arg1 (addpunct (concatword " " $arg3 " ") 1)
|
||||
] [
|
||||
add2list $arg1 (addpunct (concatword " " (addpunct $arg3) " ") 1)
|
||||
]
|
||||
]
|
||||
]
|
||||
|
||||
MDArray_Get_Bracket_Positions = [
|
||||
open_bracket_pos = (+ (strpos $arg1 $arg2) (strlen $arg2))
|
||||
close_bracket_pos = -1
|
||||
|
||||
loop i (- (strlen $arg1) $open_bracket_pos) [
|
||||
next_char = (+ (+ $open_bracket_pos $i) 1)
|
||||
if (strstr (substr $arg1 $next_char 1) "]") [
|
||||
close_bracket_pos = $next_char
|
||||
break
|
||||
]
|
||||
]
|
||||
]
|
||||
|
||||
MDArray_Get = [
|
||||
mdarray = (getalias $arg1)
|
||||
target = (findlist $mdarray $arg2)
|
||||
|
||||
if (!= $target -1) [
|
||||
MDArray_Get_Bracket_Positions $mdarray $arg2
|
||||
|
||||
if (!= $close_bracket_pos -1) [
|
||||
+= open_bracket_pos 1 // skip the bracket itself when extracting the subarray with the line below
|
||||
subarray = (substr $mdarray $open_bracket_pos (- $close_bracket_pos $open_bracket_pos))
|
||||
subarray = (substr $subarray 2 (- (strlen $subarray) 3)) // trim brackets/whitespace from the subarray
|
||||
subarray = (substr $subarray 1 (- (strlen $subarray) 1)) // trim first quotation from the subarray
|
||||
subarray = (substr $subarray 0 (- (strlen $subarray) 1)) // trim last quotation from the subarray
|
||||
|
||||
// double check that the first and last chars of the subarray are not quotations
|
||||
firstchar = (substr $subarray 0 1)
|
||||
lastchar = (substr $subarray (- (strlen $subarray) 1) 1)
|
||||
// trim quotations
|
||||
if (! (hasQuotePair $subarray)) [ // only if its not a multi-word elements array
|
||||
if (testchar $firstchar 8) [ subarray = (substr $subarray 1 (- (strlen $subarray) 1)) ]
|
||||
if (testchar $lastchar 8) [ subarray = (substr $subarray 0 (- (strlen $subarray) 1)) ]
|
||||
]
|
||||
|
||||
if (= $arg3 -1) [
|
||||
result $subarray
|
||||
] [ result (at $subarray $arg3) ]
|
||||
]
|
||||
]
|
||||
]
|
||||
|
||||
MDArray_Get_All_Opening_Bracket_Positions = [
|
||||
o_positions = ""
|
||||
loop i (strlen $arg1) [
|
||||
if (strcmp (substr $arg1 $i 1) "[") [
|
||||
add2list o_positions $i
|
||||
]
|
||||
]
|
||||
if (listlen $o_positions) [
|
||||
result $o_positions
|
||||
] [ result -1 ]
|
||||
]
|
||||
|
||||
MDArray_Get_Subarray_Count = [
|
||||
result (listlen (MDArray_Get_All_Opening_Bracket_Positions (getalias $arg1)))
|
||||
]
|
||||
|
||||
MDArray_Get_All_Subarray_Names = [
|
||||
opening_brackets = (MDArray_Get_All_Opening_Bracket_Positions (getalias $arg1))
|
||||
subarray_names = ""
|
||||
|
||||
loop r (listlen $opening_brackets) [
|
||||
add2list subarray_names (extractSubarrayName (getalias $arg1) (at $opening_brackets $r))
|
||||
]
|
||||
result $subarray_names
|
||||
]
|
||||
|
||||
MDArray_Search = [
|
||||
mdarray = (getalias $arg1)
|
||||
token = $arg2
|
||||
subarrays = (MDArray_Get_All_Subarray_Names $arg1)
|
||||
search_results = ""
|
||||
|
||||
loop u (listlen $subarrays) [
|
||||
curarray = (MDArray_Get $arg1 (at $subarrays $u) -1)
|
||||
if (strstr $curarray $token) [
|
||||
if (! (strstr $search_results (at $subarrays $u))) [
|
||||
add2list search_results (at $subarrays $u)
|
||||
]
|
||||
]
|
||||
]
|
||||
result $search_results
|
||||
]
|
||||
|
||||
MDArray_Delete_Subarray = [
|
||||
orig_array = (getalias $arg1)
|
||||
target_subarray = $arg2
|
||||
target_pos = (strpos $orig_array $target_subarray)
|
||||
gotclose = -1
|
||||
delstr = ""
|
||||
|
||||
if (!= $target_pos -1) [
|
||||
loop h (strlen $orig_array) [
|
||||
if (< $h $target_pos) continue [
|
||||
if (strcmp (substr $orig_array $h 1) "]") [
|
||||
gotclose = $h; break
|
||||
]
|
||||
]
|
||||
]
|
||||
delstr = (substr $orig_array $target_pos (- $gotclose $target_pos))
|
||||
$arg1 = (strreplace $orig_array $delstr "")
|
||||
result (getalias $arg1)
|
||||
]
|
||||
]
|
||||
|
||||
MDArray_Modify_Subarray = [
|
||||
original_subarray = (MDArray_Get $arg1 $arg2 -1)
|
||||
target_elem = $arg3
|
||||
new_elem = $arg4
|
||||
new_subarray = ""
|
||||
|
||||
loop j (listlen $original_subarray) [
|
||||
next_elem = (at $original_subarray $j)
|
||||
|
||||
if (= $j $target_elem) [
|
||||
if (> (listlen $new_elem) 1) [
|
||||
add2list new_subarray (addpunct $new_elem)
|
||||
] [ add2list new_subarray $new_elem ]
|
||||
] [
|
||||
if (> (listlen $next_elem) 1) [
|
||||
add2list new_subarray (addpunct $next_elem)
|
||||
] [ add2list new_subarray $next_elem ]
|
||||
]
|
||||
]
|
||||
|
||||
MDArray_Delete_Subarray $arg1 $arg2
|
||||
if (islists $new_subarray) [
|
||||
MDArray_New_Subarray $arg1 $arg2 (addpunct $new_subarray $brackets)
|
||||
] [
|
||||
MDArray_New_Subarray $arg1 $arg2 (addpunct $new_subarray)
|
||||
]
|
||||
]
|
||||
|
||||
// some of these aliases can cause issues if they get stored in saved.cfg (missing bracket errors/etc.)
|
||||
addListOnQuit "bool MDArray_New_Subarray MDArray_Get_Bracket_Positions MDArray_Get reverseString MDArray_Get_All_Opening_Bracket_Positions MDArray_Get_Subarray_Count MDArray_Get_All_Subarray_Names extractSubarrayName MDArray_Search MDArray_Delete_Subarray MDArray_Modify_Subarray original_subarray target_elem new_elem new_subarray j next_elem islists hasQuotePair got_open_quote got_close_quote r i d mdarray subarray firstchar lastchar oldstr newstr subarray_names subarray_name tmpchar token subarrays search_results curarray orig_array target_subarray target_pos gotclose delstr h next_char"
|
||||
+2
-1
@@ -109,7 +109,7 @@ const NUM_WEAPONS (listlen $WEAPONS)
|
||||
const MODES "tdm coop dm surv ts ctf pf btdm bdm lss osok tosok bosok htf tktf ktf tpf tlss bpf blss bts btosok"
|
||||
const NUM_MODES (listlen $MODES)
|
||||
// ktf ac_mines - calls a vote for keep the flag ac_mines
|
||||
// echo (ktf -1) = returns 15
|
||||
// echo (ktf -1) - returns 15
|
||||
|
||||
loop d $NUM_WEAPONS [ const (at $WEAPONS $d) $d ]
|
||||
loop r $NUM_MODES [ const (at $MODES $r) (addpunct (format "if (= $arg1 -1) [ result %1 ] [ votemap %1 %2 ]" $r (addpunct arg1 3)) 2) ]
|
||||
@@ -385,6 +385,7 @@ const customclaimadminscript [
|
||||
|
||||
// Execute other .cfgs/scripts.
|
||||
run on_quit
|
||||
run mdarray
|
||||
run dyngamma
|
||||
run survival
|
||||
run autosave
|
||||
|
||||
@@ -1271,6 +1271,158 @@ echo foo
|
||||
</references>
|
||||
</scriptalias>
|
||||
|
||||
<command name="MDArray_New_Subarray">
|
||||
<description>Adds a subarray to a cubescript multidimensional array. (alias)</description>
|
||||
<examples>
|
||||
<example>
|
||||
<code><![CDATA[MDArray_New_Subarray test words "how now brown cow"; echo (MDArray_Get test words -1); echo (MDArray_Get test words 2)]]></code>
|
||||
<explanation>Output: how now brown cow</explanation>
|
||||
<explanation>Output: brown</explanation>
|
||||
</example>
|
||||
</examples>
|
||||
<arguments>
|
||||
<argument token="A" description="name of alias (multidimensional array)"/>
|
||||
<argument token="N" description="name of subarray to create"/>
|
||||
<argument token="C" description="subarray content (string)"/>
|
||||
</arguments>
|
||||
<references>
|
||||
<identifierReference identifier="MDArray_Get"/>
|
||||
<identifierReference identifier="MDArray_Modify_Subarray"/>
|
||||
<identifierReference identifier="MDArray_Delete_Subarray"/>
|
||||
<identifierReference identifier="MDArray_Get_Subarray_Count"/>
|
||||
<identifierReference identifier="MDArray_Get_All_Subarray_Names"/>
|
||||
<identifierReference identifier="MDArray_Search"/>
|
||||
</references>
|
||||
</command>
|
||||
|
||||
<command name="MDArray_Get">
|
||||
<description>Returns a subarray or a subarray element of a cubescript multidimensional array.</description>
|
||||
<examples>
|
||||
<example>
|
||||
<code><![CDATA[MDArray_New_Subarray test words "how now brown cow"; echo (MDArray_Get test words -1); echo (MDArray_Get test words 2)]]></code>
|
||||
<explanation>Output: how now brown cow</explanation>
|
||||
<explanation>Output: brown</explanation>
|
||||
</example>
|
||||
</examples>
|
||||
<arguments>
|
||||
<argument token="A" description="name of alias (multidimensional array)"/>
|
||||
<argument token="N" description="name of target subarray to get"/>
|
||||
<argument token="E" description="element # of subarray to get (-1 to return the whole subarray)"/>
|
||||
</arguments>
|
||||
<references>
|
||||
<identifierReference identifier="MDArray_New_Subarray"/>
|
||||
<identifierReference identifier="MDArray_Modify_Subarray"/>
|
||||
<identifierReference identifier="MDArray_Delete_Subarray"/>
|
||||
<identifierReference identifier="MDArray_Get_Subarray_Count"/>
|
||||
<identifierReference identifier="MDArray_Get_All_Subarray_Names"/>
|
||||
<identifierReference identifier="MDArray_Search"/>
|
||||
</references>
|
||||
</command>
|
||||
|
||||
<command name="MDArray_Modify_Subarray">
|
||||
<description>Modifies a subarray element of a cubescript multidimensional array.</description>
|
||||
<examples>
|
||||
<example>
|
||||
<code><![CDATA[MDArray_New_Subarray test numbers "1 2 3 4 5"; MDArray_Modify_Subarray test numbers 2 9; echo (MDArray_Get test numbers -1)]]></code>
|
||||
<explanation>Output: 1 2 9 4 5</explanation>
|
||||
</example>
|
||||
</examples>
|
||||
|
||||
<arguments>
|
||||
<argument token="A" description="name of alias (multidimensional array)"/>
|
||||
<argument token="N" description="name of target subarray to modify"/>
|
||||
<argument token="E" description="element # of subarray to modify"/>
|
||||
<argument token="C" description="new value of subarray element"/>
|
||||
</arguments>
|
||||
<references>
|
||||
<identifierReference identifier="MDArray_New_Subarray"/>
|
||||
<identifierReference identifier="MDArray_Get"/>
|
||||
<identifierReference identifier="MDArray_Delete_Subarray"/>
|
||||
<identifierReference identifier="MDArray_Get_Subarray_Count"/>
|
||||
<identifierReference identifier="MDArray_Get_All_Subarray_Names"/>
|
||||
<identifierReference identifier="MDArray_Search"/>
|
||||
</references>
|
||||
</command>
|
||||
|
||||
<command name="MDArray_Delete_Subarray">
|
||||
<description>Deletes an entire subarray from an existing cubescript multidimensional array.</description>
|
||||
<arguments>
|
||||
<argument token="A" description="name of alias (multidimensional array)"/>
|
||||
<argument token="N" description="name of target subarray to delete"/>
|
||||
</arguments>
|
||||
<references>
|
||||
<identifierReference identifier="MDArray_New_Subarray"/>
|
||||
<identifierReference identifier="MDArray_Get"/>
|
||||
<identifierReference identifier="MDArray_Modify_Subarray"/>
|
||||
<identifierReference identifier="MDArray_Get_Subarray_Count"/>
|
||||
<identifierReference identifier="MDArray_Get_All_Subarray_Names"/>
|
||||
<identifierReference identifier="MDArray_Search"/>
|
||||
</references>
|
||||
</command>
|
||||
|
||||
<command name="MDArray_Get_Subarray_Count">
|
||||
<description>Returns the number of subarrays in a cubescript multidimensional array.</description>
|
||||
<arguments>
|
||||
<argument token="A" description="name of alias (multidimensional array)"/>
|
||||
</arguments>
|
||||
<references>
|
||||
<identifierReference identifier="MDArray_New_Subarray"/>
|
||||
<identifierReference identifier="MDArray_Get"/>
|
||||
<identifierReference identifier="MDArray_Modify_Subarray"/>
|
||||
<identifierReference identifier="MDArray_Delete_Subarray"/>
|
||||
<identifierReference identifier="MDArray_Get_All_Subarray_Names"/>
|
||||
<identifierReference identifier="MDArray_Search"/>
|
||||
</references>
|
||||
</command>
|
||||
|
||||
<command name="MDArray_Get_All_Subarray_Names">
|
||||
<description>Returns a list of all subarray names in a cubescript multidimensional array.</description>
|
||||
<examples>
|
||||
<example>
|
||||
<code><![CDATA[MDArray_New_Subarray test chars "a b c d e f"; MDArray_New_Subarray test words "how now brown cow"; echo (MDArray_Get_All_Subarray_Names test)]]></code>
|
||||
<explanation>Output: chars words</explanation>
|
||||
</example>
|
||||
</examples>
|
||||
<arguments>
|
||||
<argument token="A" description="name of alias (multidimensional array)"/>
|
||||
</arguments>
|
||||
<references>
|
||||
<identifierReference identifier="MDArray_New_Subarray"/>
|
||||
<identifierReference identifier="MDArray_Get"/>
|
||||
<identifierReference identifier="MDArray_Modify_Subarray"/>
|
||||
<identifierReference identifier="MDArray_Delete_Subarray"/>
|
||||
<identifierReference identifier="MDArray_Get_Subarray_Count"/>
|
||||
<identifierReference identifier="MDArray_Search"/>
|
||||
</references>
|
||||
</command>
|
||||
|
||||
<command name="MDArray_Search">
|
||||
<description>Returns a list of all subarray names that have content matching a specific keyword.</description>
|
||||
<examples>
|
||||
<example>
|
||||
<code><![CDATA[
|
||||
MDArray_New_Subarray test words "how now brown cow";
|
||||
MDArray_New_Subarray test days "monday tuesday wednesday thursday friday saturday sunday";
|
||||
MDArray_New_Subarray test sentences [ "his shirt was brown" "her shirt was white" ];
|
||||
echo (MDArray_Search test brown)
|
||||
]]></code>
|
||||
<explanation>Output: words sentences</explanation>
|
||||
</example>
|
||||
</examples>
|
||||
<arguments>
|
||||
<argument token="A" description="name of alias (multidimensional array)"/>
|
||||
<argument token="K" description="keyword to search for"/>
|
||||
</arguments>
|
||||
<references>
|
||||
<identifierReference identifier="MDArray_New_Subarray"/>
|
||||
<identifierReference identifier="MDArray_Get"/>
|
||||
<identifierReference identifier="MDArray_Modify_Subarray"/>
|
||||
<identifierReference identifier="MDArray_Delete_Subarray"/>
|
||||
<identifierReference identifier="MDArray_Get_Subarray_Count"/>
|
||||
<identifierReference identifier="MDArray_Get_All_Subarray_Names"/>
|
||||
</references>
|
||||
</command>
|
||||
|
||||
</identifiers>
|
||||
</section>
|
||||
<!-- CubeScript language Section ends -->
|
||||
|
||||
Reference in New Issue
Block a user