Advertisement
ijontichy

commonFuncs.h

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