Advertisement
ijontichy

stralloc.c

Apr 29th, 2012
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 15.34 KB | None | 0 0
  1. #include "zcommon.acs"
  2. #include "commonFuncs.h"
  3.  
  4. #define STRALLOC_INPUTSTRING    192
  5. #define STRALLOC_INPUTHELP      193
  6.  
  7. #define INPUTIDBASE 29050
  8. #define INPUTCHARS  95
  9.  
  10. global int 0:strings[];
  11. global int 9:stringIndex[];
  12.  
  13. int vInputChars[INPUTCHARS] = {97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 58, 59, 60, 61, 62, 63, 64, 91, 92, 93, 94, 95, 96, 123, 124, 125, 126, 32};
  14.  
  15. int vOutputChars[128] = {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 94, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 77, 78, 79, 80, 81, 82, 83, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 84, 85, 86, 87, 88, 89, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 90, 91, 92, 93, -1};
  16.  
  17. int inputLock[32] = {0};
  18.  
  19. function int addString(int string)
  20. {
  21.     int strSize = StrLen(string) + 1;  // gotta remember the null byte at the start
  22.  
  23.     int index = 0; int ret; int i = 0; int j; int c;
  24.    
  25.     int wIndex = findFreeIndex(strSize);
  26.  
  27.     for (i = 0; i < strSize; i++)
  28.     {
  29.         j = i + wIndex;
  30.  
  31.         strings[j] = GetChar(string, i);
  32.     }
  33.  
  34.     strings[j + i] = 0;
  35.    
  36.     addIndex(wIndex);
  37.     return wIndex;
  38. }
  39.  
  40. function int addCleanString(int string)
  41. {
  42.     int strSize = StrLen(string) + 1;
  43.  
  44.     int index = 0; int i = 0; int j = 0; int k = 0; int c; int ignoreNext;
  45.    
  46.     int wIndex = findFreeIndex(strSize);
  47.  
  48.     for (i = 0; i < strSize; i++)
  49.     {
  50.         c = GetChar(string, i);
  51.  
  52.         if ( ( ((c > 8) && (c < 14)) || ((c > 31) && (c < 127)) || ((c > 160) && (c < 173)) ) && !ignoreNext)
  53.         {
  54.             j = (k++) + wIndex;
  55.             strings[j] = c;
  56.         }
  57.         else if (c == 28 && !ignoreNext)
  58.         {
  59.             ignoreNext = 1;
  60.         }
  61.         else
  62.         {
  63.             ignoreNext = 0;
  64.         }
  65.     }
  66.  
  67.     strings[j + 1] = 0;
  68.    
  69.     addIndex(wIndex);
  70.     return wIndex;
  71. }
  72.  
  73. function int addLowerString(int string)
  74. {
  75.     int strSize = StrLen(string) + 1;
  76.  
  77.     int index = 0; int i = 0; int j = 0; int k = 0; int c; int ignoreNext;
  78.    
  79.     int wIndex = findFreeIndex(strSize);
  80.  
  81.     for (i = 0; i < strSize; i++)
  82.     {
  83.         c = GetChar(string, i);
  84.  
  85.         if ( ( ((c > 8) && (c < 14)) || ((c > 31) && (c < 127)) || ((c > 160) && (c < 173)) ) && !ignoreNext)
  86.         {
  87.             j = (k++) + wIndex;
  88.  
  89.             if ((c > 64) && (c < 91))
  90.             {
  91.                 strings[j] = c + 32;
  92.             }
  93.             else
  94.             {
  95.                 strings[j] = c;
  96.             }
  97.         }
  98.         else if (c == 28 && !ignoreNext)
  99.         {
  100.             ignoreNext = 1;
  101.         }
  102.         else
  103.         {
  104.             ignoreNext = 0;
  105.         }
  106.     }
  107.  
  108.     strings[j + 1] = 0;
  109.    
  110.     addIndex(wIndex);
  111.     return wIndex;
  112. }
  113.  
  114. function int getString(int index)
  115. {
  116.     int ret = "";
  117.     int i = 0; int j; int c;
  118.    
  119.     if (getIndex(index) == -1)
  120.     {
  121.         return StrParam(s:"ERR");
  122.     }
  123.  
  124.     while (1)
  125.     {
  126.         j = i + index;
  127.         i += 1;
  128.  
  129.         c = strings[j];
  130.  
  131.         if (c == 0)
  132.         {
  133.             break;
  134.         }
  135.  
  136.         ret = StrParam(s:ret, c:c);
  137.     }
  138.    
  139.     return ret;
  140. }
  141.  
  142. function void freeString(int index)
  143. {
  144.     int i = 0; int j; int c;
  145.    
  146.     if (getIndex(index) == -1)
  147.     {
  148.         return;
  149.     }
  150.  
  151.     while (1)
  152.     {
  153.         j = i + index;
  154.  
  155.         c = strings[j];
  156.  
  157.         if (c == 0)
  158.         {
  159.             break;
  160.         }
  161.  
  162.         strings[j] = 0;
  163.         i += 1;
  164.     }
  165.  
  166.     freeIndex(index);
  167. }
  168.  
  169. function int stringLength(int index)
  170. {
  171.     int i = 0; int j; int c;
  172.    
  173.     if (getIndex(index) == -1)
  174.     {
  175.         return -1;
  176.     }
  177.  
  178.     while (1)
  179.     {
  180.         j = i + index;
  181.  
  182.         c = strings[j];
  183.  
  184.         if (c == 0)
  185.         {
  186.             break;
  187.         }
  188.        
  189.         i += 1;
  190.     }
  191.    
  192.     return i;
  193. }
  194.  
  195. function int reallocString(int index, int string)
  196. {
  197.     freeString(index);
  198.     return addString(string);
  199. }
  200.  
  201. function int reallocCleanString(int index, int string)
  202. {
  203.     freeString(index);
  204.     return addCleanString(string);
  205. }
  206.  
  207. function int reallocLowerString(int index, int string)
  208. {
  209.     freeString(index);
  210.     return addLowerString(string);
  211. }
  212.  
  213.  
  214.  
  215. function int findFreeIndex(int size)
  216. {
  217.     int c; int i = -1; int index = 0; int ret;
  218.  
  219.     while (1)
  220.     {
  221.         c = strings[index];
  222.        
  223.         if (c == 0)
  224.         {
  225.             if (i == 0)
  226.             {
  227.                 ret = index;
  228.             }
  229.  
  230.             i += 1;
  231.         }
  232.         else
  233.         {
  234.             i = -1;
  235.         }
  236.  
  237.         if (i >= (size - 1))
  238.         {
  239.             break;
  240.         }
  241.  
  242.         index += 1;
  243.     }
  244.  
  245.     return ret;
  246. }
  247.  
  248. function int addIndex(int index)
  249. {
  250.     int i = 0;
  251.     while (1)
  252.     {
  253.         if (stringIndex[i] == (index + 1))
  254.         {
  255.             break;
  256.         }
  257.         else if (stringIndex[i] == 0)
  258.         {
  259.             stringIndex[i] = (index + 1);
  260.             break;
  261.         }
  262.         i += 1;
  263.     }
  264.  
  265.     return i;
  266. }
  267.  
  268. function int getIndex(int index)
  269. {
  270.     int i = 0;
  271.  
  272.     while (1)
  273.     {
  274.         if (stringIndex[i] == (index + 1))
  275.         {
  276.             break;
  277.         }
  278.         else if (stringIndex[i] == 0)
  279.         {
  280.             return -1;
  281.         }
  282.  
  283.         i += 1;
  284.     }
  285.  
  286.     return i;
  287. }
  288.  
  289. function void freeIndex(int index)
  290. {
  291.     int curIndex = getIndex(index);
  292.    
  293.     if (curIndex == -1)
  294.     {
  295.         return;
  296.     }
  297.  
  298.     int i = curIndex + 1;
  299.     int j = i;
  300.  
  301.     stringIndex[curIndex] = 0;
  302.  
  303.     while (1)
  304.     {
  305.         if (stringIndex[i] == 0)
  306.         {
  307.             break;
  308.         }
  309.         j = i;
  310.         i += 1;
  311.     }
  312.    
  313.     stringIndex[curIndex] = stringIndex[j];
  314.     stringIndex[j] = 0;
  315. }
  316.  
  317.  
  318. function int dumpStrings(int stop)
  319. {
  320.     int printStr = "";
  321.     int i = 0; int strsFound = 0; int stopAtZero = 0;
  322.    
  323.     if (stop == 0)
  324.     {
  325.         stop = 0x7FFFFFFF;
  326.         stopAtZero = 1;
  327.     }
  328.  
  329.     while (i < stop)
  330.     {
  331.         if (stringIndex[i] != 0)
  332.         {
  333.             strsFound++;
  334.             printStr = StrParam(s:printStr, s:"(", d:i, s:", ", d:stringIndex[i], s:"): ", s:getString(stringIndex[i] - 1), s:"\n");
  335.         }
  336.         else if (stopAtZero)
  337.         {
  338.             break;
  339.         }
  340.         i += 1;
  341.     }
  342.    
  343.     if (strsFound == 0)
  344.     {
  345.         printStr = StrParam(s:"<NONE>"); // For consistency reasons, we use StrParam here
  346.     }
  347.  
  348.     return printStr;
  349. }
  350.  
  351. function int padStringR(int baseStr, int padChar, int len)
  352. {
  353.     int baseStrLen = StrLen(baseStr);
  354.     int pad = "";
  355.     int padLen; int i;
  356.  
  357.     if (baseStrLen >= len)
  358.     {
  359.         return baseStr;
  360.     }
  361.    
  362.     padChar = GetChar(padChar, 0);
  363.     padLen = len - baseStrLen;
  364.  
  365.     for (i = 0; i < padLen; i++)
  366.     {
  367.         pad = StrParam(s:pad, c:padChar);
  368.     }
  369.  
  370.     return StrParam(s:baseStr, s:pad);
  371. }
  372.  
  373. function int padStringL(int baseStr, int padChar, int len)
  374. {
  375.     int baseStrLen = StrLen(baseStr);
  376.     int pad = "";
  377.     int padLen; int i;
  378.  
  379.     if (baseStrLen >= len)
  380.     {
  381.         return baseStr;
  382.     }
  383.    
  384.     padChar = GetChar(padChar, 0);
  385.     padLen = len - baseStrLen;
  386.  
  387.     for (i = 0; i < padLen; i++)
  388.     {
  389.         pad = StrParam(s:pad, c:padChar);
  390.     }
  391.  
  392.     return StrParam(s:pad, s:baseStr);
  393. }
  394.  
  395. function int changeString(int string, int repl, int where)
  396. {
  397.     int i; int j; int k;
  398.     int ret = "";
  399.     int len = StrLen(string);
  400.     int rLen = StrLen(repl);
  401.  
  402.     if ((where + rLen < 0) || (where >= len))
  403.     {
  404.         return string;
  405.     }
  406.    
  407.     for (i = 0; i < len; i++)
  408.     {
  409.         if (inRange(where, where+rLen, i))
  410.         {
  411.             ret = StrParam(s:ret, c:GetChar(repl, i-where));
  412.         }
  413.         else
  414.         {
  415.             ret = StrParam(s:ret, c:GetChar(string, i));
  416.         }
  417.     }
  418.  
  419.     return ret;
  420. }
  421.  
  422. function int sliceString(int string, int start, int end)
  423. {
  424.     int len = StrLen(string);
  425.     int ret = "";
  426.     int i;
  427.  
  428.     if (start < 0)
  429.     {
  430.         start = len + start;
  431.     }
  432.  
  433.     if (end <= 0)
  434.     {
  435.         end = len + end;
  436.     }
  437.  
  438.     start = max(0, start);
  439.     end   = min(end, len-1);
  440.    
  441.     for (i = start; i < end; i++)
  442.     {
  443.         ret = StrParam(s:ret, c:GetChar(string, i));
  444.     }
  445.  
  446.     return ret;
  447. }
  448.  
  449.  
  450. script STRALLOC_INPUTSTRING (int maxLength, int cStr, int prompt)
  451. {
  452.     int cCharIndex = 0;
  453.     int cChar = vInputChars[cCharIndex];
  454.     int cStrIndex = 0;
  455.     int pStr; int pStr2; int pStrLen;
  456.     int pChar; int pCharStr;
  457.    
  458.     int yawBuffer;
  459.     int yawOverflow;
  460.     int letterShift;
  461.    
  462.     int pitchBuffer;
  463.     int pitchOverflow;
  464.     int charShift;
  465.  
  466.     int hudWidth; int cenWidth;
  467.     int hudHeight; int cenHeight;
  468.     int update; int tic = 0;
  469.     int cStrTmp;
  470.  
  471.     int pln = PlayerNumber();
  472.  
  473.     if (pln == -1)
  474.     {
  475.         terminate;
  476.     }
  477.  
  478.     if (inputLock[pln] == 1)
  479.     {
  480.         terminate;
  481.     }
  482.     inputLock[pln] = 1;
  483.     SetPlayerProperty(0, 1, PROP_TOTALLYFROZEN);
  484.  
  485.     if (cStr == 0)
  486.     {
  487.         cStr = addString("");
  488.     }
  489.     else
  490.     {
  491.         cStr = addString(cStr);
  492.         cStrTmp = getString(cStr);
  493.  
  494.         cCharIndex = GetChar(cStrTmp, cStrIndex);
  495.         cCharIndex = vOutputChars[cCharIndex];
  496.     }
  497.  
  498.     maxLength = max(0, maxLength);
  499.  
  500.     if (maxLength == 0)
  501.     {
  502.        maxLength = 32;
  503.     }
  504.    
  505.     hudWidth  = max(320, 10 * maxLength);
  506.     hudHeight = hudWidth - (hudWidth >> 2);
  507.     cenWidth  = (hudWidth << 15) + 0.4;
  508.     cenHeight = (hudHeight << 15);
  509.  
  510.     SetHudSize(hudWidth, hudHeight, 1);
  511.     SetFont("CONFONT");
  512.  
  513.     while (1)
  514.     {
  515.         letterShift = 0;
  516.         charShift = 0;
  517.  
  518.         if (keyPressed(BT_MOVERIGHT) || keyPressed(BT_RIGHT) || keyPressed(BT_ATTACK))
  519.         {
  520.             letterShift++;
  521.         }
  522.         else if (keyPressed(BT_MOVELEFT) || keyPressed(BT_LEFT) || keyPressed(BT_ALTATTACK))
  523.         {
  524.             letterShift--;
  525.         }
  526.  
  527.         if (keyPressed(BT_FORWARD))
  528.         {
  529.             charShift++;
  530.         }
  531.         else if (keyPressed(BT_BACK))
  532.         {
  533.             charShift--;
  534.         }
  535.        
  536.         pitchBuffer += GetPlayerInput(-1, INPUT_PITCH) * 150;
  537.         pitchOverflow = pitchBuffer >> 16;
  538.         pitchBuffer -= (pitchOverflow << 16);
  539.        
  540.         if (pitchOverflow + charShift != 0)
  541.         {
  542.             cCharIndex += (pitchOverflow + charShift);
  543.  
  544.             charShift = 0; pitchOverflow = 0;
  545.             update = 1;
  546.         }
  547.        
  548.         if (yawOverflow + letterShift != 0)
  549.         {
  550.             cStrTmp = getString(cStr);
  551.             cStrIndex += (yawOverflow + letterShift);
  552.  
  553.             cStrIndex = max(0, cStrIndex);
  554.  
  555.             if (cStrIndex >= StrLen(cStrTmp))
  556.             {
  557.                 cCharIndex = 0;
  558.             }
  559.             else
  560.             {
  561.                 cCharIndex = GetChar(cStrTmp, cStrIndex);
  562.                 cCharIndex = vOutputChars[cCharIndex];
  563.             }
  564.  
  565.             letterShift = 0; yawOverflow = 0;
  566.             update = 1;
  567.         }
  568.  
  569.  
  570.         if (keyPressed(BT_JUMP))
  571.         {
  572.             cStrTmp = getString(cStr);
  573.             cStrTmp = sliceString(cStrTmp, 0, -1);
  574.             cStr = reallocString(cStr, cStrTmp);
  575.  
  576.             if ((cStrIndex >= StrLen(cStrTmp)) && (cStrIndex != 0))
  577.             {
  578.                 cStrIndex = StrLen(cStrTmp) - 1;
  579.  
  580.                 cCharIndex = GetChar(cStrTmp, cStrIndex);
  581.                 cCharIndex = vOutputChars[cCharIndex];
  582.             }
  583.  
  584.             update = 1;
  585.         }
  586.        
  587.         if (keyPressed(BT_CROUCH))
  588.         {
  589.             cStr = reallocString(cStr, "");
  590.             cStrIndex = 0;
  591.  
  592.             update = 1;
  593.         }
  594.  
  595.         if (keyPressed(BT_USE))
  596.         {
  597.             HudMessage(s:""; HUDMSG_PLAIN, INPUTIDBASE-3, CR_WHITE, 0, 0, 1);
  598.             HudMessage(s:""; HUDMSG_PLAIN, INPUTIDBASE-2, CR_WHITE, 0, 0, 1);
  599.             HudMessage(s:""; HUDMSG_PLAIN, INPUTIDBASE-1, CR_WHITE, 0, 0, 1);
  600.             HudMessage(s:""; HUDMSG_PLAIN, INPUTIDBASE,   CR_WHITE, 0, 0, 1);
  601.             HudMessage(s:""; HUDMSG_PLAIN, INPUTIDBASE+1, CR_WHITE, 0, 0, 1);
  602.             HudMessage(s:""; HUDMSG_PLAIN, INPUTIDBASE+2, CR_WHITE, 0, 0, 1);
  603.             HudMessage(s:""; HUDMSG_PLAIN, INPUTIDBASE+3, CR_WHITE, 0, 0, 1);
  604.             HudMessage(s:""; HUDMSG_PLAIN, INPUTIDBASE+4, CR_WHITE, 0, 0, 1);
  605.             HudMessage(s:""; HUDMSG_PLAIN, INPUTIDBASE+5, CR_WHITE, 0, 0, 1);
  606.             break;
  607.         }
  608.  
  609.         if (update || (tic % 175 == 0))
  610.         {
  611.             ACS_ExecuteAlways(STRALLOC_INPUTHELP, 0, prompt, 0,0);
  612.             tic = 0;
  613.             update = 0;
  614.  
  615.             cStrIndex = mod(cStrIndex, maxLength);
  616.  
  617.             cStrTmp = getString(cStr);
  618.  
  619.             if (StrLen(cStrTmp) <= cStrIndex)
  620.             {
  621.                 cCharIndex = 0;
  622.             }
  623.  
  624.             cCharIndex = mod(cCharIndex, INPUTCHARS);
  625.             cChar   = vInputChars[cCharIndex];
  626.  
  627.             cStrTmp = padStringR(cStrTmp, " ", cStrIndex+1);
  628.             cStr    = reallocString(cStr, changeString(cStrTmp, StrParam(c:cChar), cStrIndex));
  629.             cStrTmp = getString(cStr);
  630.  
  631.             pStr    = padStringR(cStrTmp, "_", maxLength);
  632.             pStr2   = padStringR(cStrTmp, " ", maxLength);
  633.             pStrLen = StrLen(pStr);
  634.  
  635.             pChar     = StrParam(c:GetChar(pStr, cStrIndex));
  636.             pCharStr  = padStringL(pChar, " ", cStrIndex+1);
  637.             pCharStr  = padStringR(pCharStr, " ", maxLength);
  638.  
  639.             HudMessage(s:pStr;  HUDMSG_PLAIN, INPUTIDBASE, CR_BLACK, cenWidth, cenHeight, 5.0);
  640.             HudMessage(s:pStr2; HUDMSG_PLAIN, INPUTIDBASE-1, CR_GREY, cenWidth, cenHeight, 5.0);
  641.             HudMessage(s:pCharStr; HUDMSG_PLAIN, INPUTIDBASE-2, CR_GREEN, cenWidth, cenHeight, 5.0);
  642.             HudMessage(d:StrLen(cStrTmp), s:"/", d:maxLength, s:"\ca characters"; HUDMSG_PLAIN, INPUTIDBASE-3, CR_GOLD, cenWidth, cenHeight - (cenHeight >> 2), 5.0);
  643.         }
  644.        
  645.         tic++;
  646.         Delay(1);
  647.     }
  648.  
  649.     GiveInventory("192Return", cStr);
  650.  
  651.     SetPlayerProperty(0, 0, PROP_TOTALLYFROZEN);
  652.     inputLock[pln] = 0;
  653.  
  654.     Delay(1);
  655.     TakeInventory("192Return", cStr);
  656. }
  657.  
  658. script STRALLOC_INPUTHELP (int prompt)
  659. {
  660.     SetHudSize(640, 480, 0);
  661.  
  662.     HudMessage(s:"Strafing and (alt-)firing shifts characters"; HUDMSG_FADEOUT, INPUTIDBASE+1, CR_GREEN, 320.4, 320.0, 8.0, 1.0);
  663.     HudMessage(s:"Moving forward and looking up changes to next character"; HUDMSG_FADEOUT, INPUTIDBASE+2, CR_WHITE, 320.4, 335.0, 8.0, 1.0);
  664.     HudMessage(s:"Moving backward and looking down changes to previous character"; HUDMSG_FADEOUT, INPUTIDBASE+3, CR_BRICK, 320.4, 350.0, 8.0, 1.0);
  665.     HudMessage(s:"Jumping backspaces, crouch resets, use submits"; HUDMSG_FADEOUT, INPUTIDBASE+4, CR_YELLOW, 320.4, 365.0, 8.0, 1.0);
  666.  
  667.     SetHudSize(480, 360, 0);
  668.     SetFont("BIGFONT");
  669.  
  670.     if (prompt != 0)
  671.     {
  672.         HudMessage(s:prompt; HUDMSG_FADEOUT, INPUTIDBASE+5, CR_WHITE, 240.4, 112.0, 8.0, 1.0);
  673.     }
  674. }
  675.  
  676. script 293 (void)
  677. {
  678.     int s1; int s2; int s3;
  679.  
  680.     ACS_ExecuteAlways(STRALLOC_INPUTSTRING, 0, 32, "Lasers", "Mustache");
  681.    
  682.     while (CheckInventory("192Return") == 0)
  683.     {
  684.         Delay(1);
  685.     }
  686.  
  687.     s1 = CheckInventory("192Return");
  688.     s2 = addCleanString(StrParam(n:0));
  689.     s3 = addLowerString(StrParam(n:0));
  690.  
  691.     SetFont("CONFONT");
  692.  
  693.     Print(s:dumpStrings(0));
  694. }
  695.  
  696. script 294 (void)
  697. {
  698.     int s1;
  699.  
  700.     Print(s:"this SHOULD work");
  701.  
  702.     s1 = addString("this SHOULD work");
  703.  
  704.     Print(d:s1, s:": ", s:getString(s1));
  705.  
  706.     Delay(35);
  707.  
  708.     Print(s:changeString(getString(s1), "pies", 0));
  709.  
  710.     Delay(35);
  711.  
  712.     Print(s:changeString(getString(s1), "is not", 5));
  713. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement