Advertisement
ijontichy

commonFuncs.h

Sep 12th, 2012
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 13.02 KB | None | 0 0
  1.  
  2. // A bunch of functions that I've built up
  3. // They come in handy :>
  4.  
  5. function int abs(int x)
  6. {
  7.     if (x < 0) { return -x; }
  8.     return x;
  9. }
  10.  
  11. function int sign(int x)
  12. {
  13.     if (x < 0) { return -1; }
  14.     return 1;
  15. }
  16.  
  17. function int randSign(void)
  18. {
  19.     return (2*random(0,1))-1;
  20. }
  21.  
  22. function int mod(int x, int y)
  23. {
  24.     int ret = x - ((x / y) * y);
  25.     if (ret < 0) { ret = y + ret; }
  26.     return ret;
  27. }
  28.  
  29. function int pow(int x, int y)
  30. {
  31.     int n = 1;
  32.     while (y-- > 0) { n *= x; }
  33.     return n;
  34. }
  35.  
  36. function int powFloat(int x, int y)
  37. {
  38.     int n = 1.0;
  39.     while (y-- > 0) { n = FixedMul(n, x); }
  40.     return n;
  41. }
  42.  
  43. function int min(int x, int y)
  44. {
  45.     if (x < y) { return x; }
  46.     return y;
  47. }
  48.  
  49. function int max (int x, int y)
  50. {
  51.     if (x > y) { return x; }
  52.     return y;
  53. }
  54.  
  55. function int middle(int x, int y, int z)
  56. {
  57.     if ((x < z) && (y < z)) { return max(x, y); }
  58.     return max(min(x, y), z);
  59. }
  60.  
  61. function int percFloat(int intg, int frac)
  62. {
  63.     return (intg << 16) + ((frac << 16) / 100);
  64. }
  65.  
  66. function int percFloat2(int intg, int frac1, int frac2)
  67. {
  68.     return itof(intg) + (itof(frac1) / 100) + (itof(frac2) / 10000);
  69. }
  70.  
  71. function int keyUp(int key)
  72. {
  73.     int buttons = GetPlayerInput(-1, INPUT_BUTTONS);
  74.  
  75.     if ((~buttons & key) == key) { return 1; }
  76.     return 0;
  77. }
  78.  
  79. function int keyDown(int key)
  80. {
  81.     int buttons = GetPlayerInput(-1, INPUT_BUTTONS);
  82.  
  83.     if ((buttons & key) == key) { return 1; }
  84.     return 0;
  85. }
  86.  
  87. function int keyPressed(int key)
  88. {
  89.     int buttons     = GetPlayerInput(-1, INPUT_BUTTONS);
  90.     int oldbuttons  = GetPlayerInput(-1, INPUT_OLDBUTTONS);
  91.     int newbuttons  = (buttons ^ oldbuttons) & buttons;
  92.  
  93.     if ((newbuttons & key) == key) { return 1; }
  94.     return 0;
  95. }
  96.  
  97. function int adjustBottom(int tmin, int tmax, int i)
  98. {
  99.     if (tmin > tmax)
  100.     {
  101.         tmax ^= tmin; tmin ^= tmax; tmax ^= tmin;  // XOR swap
  102.     }
  103.  
  104.     if (i < tmin) { tmin = i; }
  105.     if (i > tmax) { tmin += (i - tmax); }
  106.  
  107.     return tmin;
  108. }
  109.  
  110. function int adjustTop(int tmin, int tmax, int i)
  111. {
  112.     if (tmin > tmax)
  113.     {
  114.         tmax ^= tmin; tmin ^= tmax; tmax ^= tmin;
  115.     }
  116.  
  117.     if (i < tmin) { tmax -= (tmin - i); }
  118.     if (i > tmax) { tmax = i; }
  119.  
  120.     return tmax;
  121. }
  122.  
  123. function int adjustShort(int tmin, int tmax, int i)
  124. {
  125.     if (tmin > tmax)
  126.     {
  127.         tmax ^= tmin; tmin ^= tmax; tmax ^= tmin;
  128.     }
  129.  
  130.     if (i < tmin)
  131.     {
  132.         tmax -= (tmin - i);
  133.         tmin = i;
  134.     }
  135.     if (i > tmax)
  136.     {
  137.         tmin += (i - tmax);
  138.         tmax = i;
  139.     }
  140.    
  141.     return packShorts(tmin, tmax);
  142. }
  143.  
  144.  
  145. // Taken from http://zdoom.org/wiki/sqrt
  146.  
  147. function int sqrt_i(int number)
  148. {
  149.     if (number <= 3) { return number > 0; }
  150.  
  151.     int oldAns = number >> 1,                     // initial guess
  152.         newAns = (oldAns + number / oldAns) >> 1; // first iteration
  153.  
  154.     // main iterative method
  155.     while (newAns < oldAns)
  156.     {
  157.         oldAns = newAns;
  158.         newAns = (oldAns + number / oldAns) >> 1;
  159.     }
  160.  
  161.     return oldAns;
  162. }
  163.  
  164. function int sqrt(int number)
  165. {
  166.     if (number == 1.0) { return 1.0; }
  167.     if (number <= 0) { return 0; }
  168.     int val = 150.0;
  169.     for (int i=0; i<15; i++) { val = (val + FixedDiv(number, val)) >> 1; }
  170.  
  171.     return val;
  172. }
  173.  
  174. function int magnitudeTwo(int x, int y)
  175. {
  176.     return sqrt_i(x*x + y*y);
  177. }
  178.  
  179. function int magnitudeTwo_f(int x, int y)
  180. {
  181.     return sqrt(FixedMul(x, x) + FixedMul(y, y));
  182. }
  183.  
  184. function int magnitudeThree(int x, int y, int z)
  185. {
  186.     return sqrt_i(x*x + y*y + z*z);
  187. }
  188.  
  189. function int magnitudeThree_f(int x, int y, int z)
  190. {
  191.     return sqrt(FixedMul(x, x) + FixedMul(y, y) + FixedMul(z, z));
  192. }
  193.  
  194.  
  195. function int quadPos(int a, int b, int c)
  196. {
  197.     int s1 = sqrt(FixedMul(b, b)-(4*FixedMul(a, c)));
  198.     int s2 = (2 * a);
  199.     int b1 = FixedDiv(-b + s1, s2);
  200.  
  201.     return b1;
  202. }
  203.  
  204. function int quadNeg(int a, int b, int c)
  205. {
  206.     int s1 = sqrt(FixedMul(b, b)-(4*FixedMul(a, c)));
  207.     int s2 = (2 * a);
  208.     int b1 = FixedDiv(-b - s1, s2);
  209.  
  210.     return b1;
  211. }
  212.  
  213. function int quad(int a, int b, int c, int y)
  214. {
  215.     return FixedMul(a, FixedMul(y, y)) + FixedMul(b, y) + c + y;
  216. }
  217.  
  218. function int quadHigh(int a, int b, int c, int x)
  219. {
  220.     return quadPos(a, b, c-x);
  221. }
  222.  
  223. function int quadLow(int a, int b, int c, int x)
  224. {
  225.     return quadNeg(a, b, c-x);
  226. }
  227.  
  228. function int inRange(int low, int high, int x)
  229. {
  230.     return ((x >= low) && (x < high));
  231. }
  232.  
  233. function int itof(int x) { return x << 16; }
  234. function int ftoi(int x) { return x >> 16; }
  235.  
  236. function void AddAmmoCapacity(int type, int add)
  237. {
  238.     SetAmmoCapacity(type, GetAmmoCapacity(type) + add);
  239. }
  240.  
  241. function int packShorts(int left, int right)
  242. {
  243.     return ((left & 0xFFFF) << 16) + (right & 0xFFFF);
  244. }
  245.  
  246. function int leftShort(int packed) { return packed >> 16; }
  247. function int rightShort(int packed) { return (packed << 16) >> 16; }
  248.  
  249.  
  250. // This stuff only works with StrParam
  251.  
  252. function int cleanString(int string)
  253. {
  254.     int ret = "";
  255.     int strSize = StrLen(string);
  256.  
  257.     int c, i, ignoreNext;
  258.    
  259.     for (i = 0; i < strSize; i++)
  260.     {
  261.         c = GetChar(string, i);
  262.  
  263.         if ( ( ((c > 8) && (c < 14)) || ((c > 31) && (c < 127)) || ((c > 160) && (c < 173)) ) && !ignoreNext)
  264.         {
  265.             ret = StrParam(s:ret, c:c);
  266.         }
  267.         else if (c == 28 && !ignoreNext)
  268.         {
  269.             ignoreNext = 1;
  270.         }
  271.         else
  272.         {
  273.             ignoreNext = 0;
  274.         }
  275.     }
  276.  
  277.     return ret;
  278. }
  279.  
  280. function int padStringR(int baseStr, int padChar, int len)
  281. {
  282.     int baseStrLen = StrLen(baseStr);
  283.     int pad = "";
  284.     int padLen; int i;
  285.  
  286.     if (baseStrLen >= len)
  287.     {
  288.         return baseStr;
  289.     }
  290.    
  291.     padChar = GetChar(padChar, 0);
  292.     padLen = len - baseStrLen;
  293.  
  294.     for (i = 0; i < padLen; i++)
  295.     {
  296.         pad = StrParam(s:pad, c:padChar);
  297.     }
  298.  
  299.     return StrParam(s:baseStr, s:pad);
  300. }
  301.  
  302. function int padStringL(int baseStr, int padChar, int len)
  303. {
  304.     int baseStrLen = StrLen(baseStr);
  305.     int pad = "";
  306.     int padLen; int i;
  307.  
  308.     if (baseStrLen >= len)
  309.     {
  310.         return baseStr;
  311.     }
  312.    
  313.     padChar = GetChar(padChar, 0);
  314.     padLen = len - baseStrLen;
  315.  
  316.     for (i = 0; i < padLen; i++)
  317.     {
  318.         pad = StrParam(s:pad, c:padChar);
  319.     }
  320.  
  321.     return StrParam(s:pad, s:baseStr);
  322. }
  323.  
  324. function int changeString(int string, int repl, int where)
  325. {
  326.     int i; int j; int k;
  327.     int ret = "";
  328.     int len = StrLen(string);
  329.     int rLen = StrLen(repl);
  330.  
  331.     if ((where + rLen < 0) || (where >= len))
  332.     {
  333.         return string;
  334.     }
  335.    
  336.     for (i = 0; i < len; i++)
  337.     {
  338.         if (inRange(where, where+rLen, i))
  339.         {
  340.             ret = StrParam(s:ret, c:GetChar(repl, i-where));
  341.         }
  342.         else
  343.         {
  344.             ret = StrParam(s:ret, c:GetChar(string, i));
  345.         }
  346.     }
  347.  
  348.     return ret;
  349. }
  350.  
  351. function int sliceString(int string, int start, int end)
  352. {
  353.     int len = StrLen(string);
  354.     int ret = "";
  355.     int i;
  356.  
  357.     if (start < 0)
  358.     {
  359.         start = len + start;
  360.     }
  361.  
  362.     if (end <= 0)
  363.     {
  364.         end = len + end;
  365.     }
  366.  
  367.     start = max(0, start);
  368.     end   = min(end, len-1);
  369.    
  370.     for (i = start; i < end; i++)
  371.     {
  372.         ret = StrParam(s:ret, c:GetChar(string, i));
  373.     }
  374.  
  375.     return ret;
  376. }
  377.  
  378. // End StrParam
  379.  
  380. function int unusedTID(int start, int end)
  381. {
  382.     int ret = start - 1;
  383.     int tidNum;
  384.  
  385.     if (start > end) { start ^= end; end ^= start; start ^= end; }  // good ol' XOR swap
  386.    
  387.     while (ret++ != end)
  388.     {
  389.         if (ThingCount(0, ret) == 0)
  390.         {
  391.             return ret;
  392.         }
  393.     }
  394.    
  395.     return -1;
  396. }
  397.  
  398. function int getMaxHealth(void)
  399. {
  400.     int maxHP = GetActorProperty(0, APROP_SpawnHealth);
  401.  
  402.     if ((maxHP == 0) && (PlayerNumber() != -1))
  403.     {
  404.         maxHP = 100;
  405.     }
  406.  
  407.     return maxHP;
  408. }
  409.  
  410. function void giveHealth(int amount)
  411. {
  412.     giveHealthFactor(amount, 1.0);
  413. }
  414.  
  415. function void giveHealthFactor(int amount, int maxFactor)
  416. {
  417.     int maxHP = ftoi(getMaxHealth() * maxFactor);
  418.     int newHP;
  419.  
  420.     int curHP = GetActorProperty(0, APROP_Health);
  421.  
  422.     if (maxFactor == 0.0) { newHP = max(curHP, curHP+amount); }
  423.     else { newHP = middle(curHP, curHP+amount, maxHP); }
  424.  
  425.     SetActorProperty(0, APROP_Health, newHP);
  426. }
  427.  
  428. function int isDead(int tid)
  429. {
  430.     return GetActorProperty(tid, APROP_Health) <= 0;
  431. }
  432.  
  433. function int isFreeForAll(void)
  434. {
  435.     if (GetCVar("terminator") || GetCVar("duel"))
  436.     {
  437.         return 1;
  438.     }
  439.  
  440.     int check1 = GetCVar("deathmatch") || GetCVar("possession") || GetCVar("lastmanstanding");
  441.     int check2 = check1 && !GetCVar("teamplay");
  442.  
  443.     return check2;
  444. }
  445.  
  446. function int isTeamGame(void)
  447. {
  448.     int ret = (GetCVar("teamplay") || GetCVar("teamgame"));
  449.     return ret;
  450. }
  451.  
  452. function int spawnDistance(int item, int dist, int tid)
  453. {
  454.     int myX, myY, myZ, myAng, myPitch, spawnX, spawnY, spawnZ;
  455.  
  456.     myX = GetActorX(0); myY = GetActorY(0); myZ = GetActorZ(0);
  457.     myAng = GetActorAngle(0); myPitch = GetActorPitch(0);
  458.  
  459.     spawnX = FixedMul(cos(myAng) * dist, cos(myPitch));
  460.     spawnX += myX;
  461.     spawnY = FixedMul(sin(myAng) * dist, cos(myPitch));
  462.     spawnY += myY;
  463.     spawnZ = myZ + (-sin(myPitch) * dist);
  464.  
  465.     return Spawn(item, spawnX, spawnY, spawnZ, tid, myAng >> 8);
  466. }
  467.  
  468. function void SetInventory(int item, int amount)
  469. {
  470.     int count = CheckInventory(item);
  471.  
  472.     if (count == amount) { return; }
  473.    
  474.     if (count > amount)
  475.     {
  476.         TakeInventory(item, count - amount);
  477.         return;
  478.     }
  479.  
  480.     GiveAmmo(item, amount - count);
  481.     return;
  482. }
  483. function void ToggleInventory(int inv)
  484. {
  485.     if (CheckInventory(inv))
  486.     {
  487.         TakeInventory(inv, 0x7FFFFFFF);
  488.     }
  489.     else
  490.     {
  491.         GiveInventory(inv, 1);
  492.     }
  493. }
  494.  
  495. function void GiveAmmo(int type, int amount)
  496. {
  497.     if (GetCVar("sv_doubleammo"))
  498.     {
  499.         int m = GetAmmoCapacity(type);
  500.         int expected = min(m, CheckInventory(type) + amount);
  501.  
  502.         GiveInventory(type, amount);
  503.         TakeInventory(type, CheckInventory(type) - expected);
  504.     }
  505.     else
  506.     {  
  507.         GiveInventory(type, amount);
  508.     }
  509. }
  510.  
  511. function void GiveActorAmmo(int tid, int type, int amount)
  512. {
  513.     if (GetCVar("sv_doubleammo"))
  514.     {
  515.         int m = GetAmmoCapacity(type);
  516.         int expected = min(m, CheckActorInventory(tid, type) + amount);
  517.  
  518.         GiveActorInventory(tid, type, amount);
  519.         TakeActorInventory(tid, type, CheckActorInventory(tid, type) - expected);
  520.     }
  521.     else
  522.     {  
  523.         GiveActorInventory(tid, type, amount);
  524.     }
  525. }
  526.  
  527. function int cond(int test, int trueRet, int falseRet)
  528. {
  529.     if (test) { return trueRet; }
  530.     return falseRet;
  531. }
  532.  
  533. function int condTrue(int test, int trueRet)
  534. {
  535.     if (test) { return trueRet; }
  536.     return test;
  537. }
  538.  
  539. function int condFalse(int test, int falseRet)
  540. {
  541.     if (test) { return test; }
  542.     return falseRet;
  543. }
  544.  
  545. function void saveCVar(int cvar, int val)
  546. {
  547.     ConsoleCommand(StrParam(s:"set ", s:cvar, s:" ", d:val));
  548.     ConsoleCommand(StrParam(s:"archivecvar ", s:cvar));
  549. }
  550.  
  551. function int defaultCVar(int cvar, int defaultVal)
  552. {
  553.     int ret = GetCVar(cvar);
  554.     if (ret == 0) { saveCVar(cvar, defaultVal); return defaultVal; }
  555.  
  556.     return ret;
  557. }
  558.  
  559.  
  560. function int onGround(int tid)
  561. {
  562.     return (GetActorZ(tid) - GetActorFloorZ(tid)) == 0;
  563. }
  564.  
  565. function int ThingCounts(int start, int end)
  566. {
  567.     int i, ret = 0;
  568.  
  569.     if (start > end) { start ^= end; end ^= start; start ^= end; }
  570.     for (i = start; i < end; i++) { ret += ThingCount(0, i); }
  571.  
  572.     return ret;
  573. }
  574.  
  575. function int PlaceOnFloor(int tid)
  576. {
  577.     if (ThingCount(0, tid) != 1) { return 1; }
  578.    
  579.     SetActorPosition(tid, GetActorX(tid), GetActorY(tid), GetActorFloorZ(tid), 0);
  580.     return 0;
  581. }
  582.  
  583. #define DIR_E  1
  584. #define DIR_NE 2
  585. #define DIR_N  3
  586. #define DIR_NW 4
  587. #define DIR_W  5
  588. #define DIR_SW 6
  589. #define DIR_S  7
  590. #define DIR_SE 8
  591.  
  592. function int getDirection(void)
  593. {
  594.     int sideMove = keyDown(BT_MOVERIGHT) - keyDown(BT_MOVELEFT);
  595.     int forwMove = keyDown(BT_FORWARD) - keyDown(BT_BACK);
  596.  
  597.     if (sideMove || forwMove)
  598.     {
  599.         switch (sideMove)
  600.         {
  601.           case -1:
  602.             switch (forwMove)
  603.             {
  604.                 case -1: return DIR_SW;
  605.                 case  0: return DIR_W;
  606.                 case  1: return DIR_NW;
  607.             }
  608.             break;
  609.  
  610.           case 0:
  611.             switch (forwMove)
  612.             {
  613.                 case -1: return DIR_S;
  614.                 case  1: return DIR_N;
  615.             }
  616.             break;
  617.  
  618.           case 1:
  619.             switch (forwMove)
  620.             {
  621.                 case -1: return DIR_SE;
  622.                 case  0: return DIR_E;
  623.                 case  1: return DIR_NE;
  624.             }
  625.             break;
  626.         }
  627.     }
  628.  
  629.     return 0;
  630. }
  631.  
  632. function int isInvulnerable(void)
  633. {
  634.     int check1 = GetActorProperty(0, APROP_Invulnerable);
  635.     int check2 = CheckInventory("PowerInvulnerable");
  636.  
  637.     return check1 || check2;
  638. }
  639.  
  640. function void saveStringCVar(int string, int cvarname)
  641. {
  642.     int slen = StrLen(string);
  643.     int i, c, cvarname2;
  644.  
  645.     for (i = 0; i < slen; i++)
  646.     {
  647.         cvarname2 = StrParam(s:cvarname, s:"_char", d:i);
  648.         SaveCVar(cvarname2, GetChar(string, i));
  649.     }
  650.  
  651.     while (1)
  652.     {
  653.         cvarname2 = StrParam(s:cvarname, s:"_char", d:i);
  654.         c = GetCVar(cvarname2);
  655.  
  656.         if (c == 0) { break; }
  657.  
  658.         ConsoleCommand(StrParam(s:"unset ", s:cvarname2));
  659.         i += 1;
  660.     }
  661. }
  662.  
  663. function int getStringCVar(int cvarname)
  664. {
  665.     int ret = "";
  666.     int i = 0, c, cvarname2;
  667.  
  668.     while (1)
  669.     {
  670.         cvarname2 = StrParam(s:cvarname, s:"_char", d:i);
  671.         c = GetCVar(cvarname2);
  672.  
  673.         if (c == 0) { break; }
  674.  
  675.         ret = StrParam(s:ret, c:c);
  676.         i += 1;
  677.     }
  678.  
  679.     return ret;
  680. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement