Advertisement
ijontichy

commonFuncs.h

Jun 22nd, 2012
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 8.49 KB | None | 0 0
  1. // A bunch of functions that I've built up
  2. // They come in handy :>
  3.  
  4. function int abs(int x)
  5. {
  6.     if (x < 0) { return -x; }
  7.     return x;
  8. }
  9.  
  10. function int mod(int x, int y)
  11. {
  12.     int ret = x - ((x / y) * y);
  13.     if (ret < 0) { ret = y + ret; }
  14.     return ret;
  15. }
  16.  
  17. function int pow(int x, int y)
  18. {
  19.     int n = 1;
  20.     while (y-- > 0) { n *= x; }
  21.     return n;
  22. }
  23.  
  24. function int powFloat(int x, int y)
  25. {
  26.     int n = 1.0;
  27.     while (y-- > 0) { n = FixedMul(n, x); }
  28.     return n;
  29. }
  30.  
  31. function int min(int x, int y)
  32. {
  33.     if (x < y) { return x; }
  34.     return y;
  35. }
  36.  
  37. function int max (int x, int y)
  38. {
  39.     if (x > y) { return x; }
  40.     return y;
  41. }
  42.  
  43. function int middle(int x, int y, int z)
  44. {
  45.     if ((x < z) && (y < z)) { return max(x, y); }
  46.     return max(min(x, y), z);
  47. }
  48.  
  49. function int percfloat(int intg, int frac)
  50. {
  51.     return (intg << 16) + ((frac << 16) / 100);
  52. }
  53.  
  54. function int keyUp(int key)
  55. {
  56.     int buttons = GetPlayerInput(-1, INPUT_BUTTONS);
  57.  
  58.     if (~buttons & key) { return 1; }
  59.     return 0;
  60. }
  61.  
  62. function int keyDown(int key)
  63. {
  64.     int buttons = GetPlayerInput(-1, INPUT_BUTTONS);
  65.  
  66.     if (buttons & key) { return 1; }
  67.     return 0;
  68. }
  69.  
  70. function int keyPressed(int key)
  71. {
  72.     int buttons     = GetPlayerInput(-1, INPUT_BUTTONS);
  73.     int oldbuttons  = GetPlayerInput(-1, INPUT_OLDBUTTONS);
  74.     int newbuttons  = (buttons ^ oldbuttons) & buttons;
  75.  
  76.     if (newbuttons & key) { return 1; }
  77.     return 0;
  78. }
  79.  
  80. function int adjustBottom(int tmin, int tmax, int i)
  81. {
  82.     if (tmin > tmax)
  83.     {
  84.         tmax ^= tmin; tmin ^= tmax; tmax ^= tmin;  // XOR swap
  85.     }
  86.  
  87.     if (i < tmin) { tmin = i; }
  88.     if (i > tmax) { tmin += (i - tmax); }
  89.  
  90.     return tmin;
  91. }
  92.  
  93. function int adjustTop(int tmin, int tmax, int i)
  94. {
  95.     if (tmin > tmax)
  96.     {
  97.         tmax ^= tmin; tmin ^= tmax; tmax ^= tmin;
  98.     }
  99.  
  100.     if (i < tmin) { tmax -= (tmin - i); }
  101.     if (i > tmax) { tmax = i; }
  102.  
  103.     return tmax;
  104. }
  105.  
  106. function int adjustShort(int tmin, int tmax, int i)
  107. {
  108.     if (tmin > tmax)
  109.     {
  110.         tmax ^= tmin; tmin ^= tmax; tmax ^= tmin;
  111.     }
  112.  
  113.     if (i < tmin)
  114.     {
  115.         tmax -= (tmin - i);
  116.         tmin = i;
  117.     }
  118.     if (i > tmax)
  119.     {
  120.         tmin += (i - tmax);
  121.         tmax = i;
  122.     }
  123.    
  124.     return packShorts(tmin, tmax);
  125. }
  126.  
  127.  
  128. // Taken from http://zdoom.org/wiki/Sqrt
  129. function int sqrt(int number)
  130. {
  131.     if (number == 1.0) { return 1.0;  }
  132.     if (number <= 0) { return 0; }
  133.     int val = 150.0;
  134.     for (int i=0; i<15; i++) { val = (val + FixedDiv(number, val)) >> 1; }
  135.  
  136.     return val;
  137. }
  138.  
  139. function int quadPos(int a, int b, int c)
  140. {
  141.     int s1 = sqrt(FixedMul(b, b)-(4*FixedMul(a, c)));
  142.     int s2 = (2 * a);
  143.     int b1 = FixedDiv(-b + s1, s2);
  144.  
  145.     return b1;
  146. }
  147.  
  148. function int quadNeg(int a, int b, int c)
  149. {
  150.     int s1 = sqrt(FixedMul(b, b)-(4*FixedMul(a, c)));
  151.     int s2 = (2 * a);
  152.     int b1 = FixedDiv(-b - s1, s2);
  153.  
  154.     return b1;
  155. }
  156.  
  157. function int quad(int a, int b, int c, int y)
  158. {
  159.     return FixedMul(a, FixedMul(y, y)) + FixedMul(b, y) + y;
  160. }
  161.  
  162. function int quadHigh(int a, int b, int c, int x)
  163. {
  164.     return quadPos(a, b, c-x);
  165. }
  166.  
  167. function int quadLow(int a, int b, int c, int x)
  168. {
  169.     return quadNeg(a, b, c-x);
  170. }
  171.  
  172. function int inRange(int low, int high, int x)
  173. {
  174.     return ((x >= low) && (x < high));
  175. }
  176.  
  177. function int itof(int x) { return x << 16; }
  178. function int ftoi(int x) { return x >> 16; }
  179.  
  180. function void AddAmmoCapacity(int type, int add)
  181. {
  182.     SetAmmoCapacity(type, GetAmmoCapacity(type) + add);
  183. }
  184.  
  185. function int packShorts(int left, int right)
  186. {
  187.     return ((left & 0xFFFF) << 16) + (right & 0xFFFF);
  188. }
  189.  
  190. function int leftShort(int packed) { return packed >> 16; }
  191. function int rightShort(int packed) { return (packed << 16) >> 16; }
  192.  
  193.  
  194. // This stuff only works with StrParam
  195.  
  196. function int cleanString(int string)
  197. {
  198.     int ret = "";
  199.     int strSize = StrLen(string);
  200.  
  201.     int c, i, ignoreNext;
  202.    
  203.     for (i = 0; i < strSize; i++)
  204.     {
  205.         c = GetChar(string, i);
  206.  
  207.         if ( ( ((c > 8) && (c < 14)) || ((c > 31) && (c < 127)) || ((c > 160) && (c < 173)) ) && !ignoreNext)
  208.         {
  209.             ret = StrParam(s:ret, c:c);
  210.         }
  211.         else if (c == 28 && !ignoreNext)
  212.         {
  213.             ignoreNext = 1;
  214.         }
  215.         else
  216.         {
  217.             ignoreNext = 0;
  218.         }
  219.     }
  220.  
  221.     return ret;
  222. }
  223.  
  224. function int padStringR(int baseStr, int padChar, int len)
  225. {
  226.     int baseStrLen = StrLen(baseStr);
  227.     int pad = "";
  228.     int padLen; int i;
  229.  
  230.     if (baseStrLen >= len)
  231.     {
  232.         return baseStr;
  233.     }
  234.    
  235.     padChar = GetChar(padChar, 0);
  236.     padLen = len - baseStrLen;
  237.  
  238.     for (i = 0; i < padLen; i++)
  239.     {
  240.         pad = StrParam(s:pad, c:padChar);
  241.     }
  242.  
  243.     return StrParam(s:baseStr, s:pad);
  244. }
  245.  
  246. function int padStringL(int baseStr, int padChar, int len)
  247. {
  248.     int baseStrLen = StrLen(baseStr);
  249.     int pad = "";
  250.     int padLen; int i;
  251.  
  252.     if (baseStrLen >= len)
  253.     {
  254.         return baseStr;
  255.     }
  256.    
  257.     padChar = GetChar(padChar, 0);
  258.     padLen = len - baseStrLen;
  259.  
  260.     for (i = 0; i < padLen; i++)
  261.     {
  262.         pad = StrParam(s:pad, c:padChar);
  263.     }
  264.  
  265.     return StrParam(s:pad, s:baseStr);
  266. }
  267.  
  268. function int changeString(int string, int repl, int where)
  269. {
  270.     int i; int j; int k;
  271.     int ret = "";
  272.     int len = StrLen(string);
  273.     int rLen = StrLen(repl);
  274.  
  275.     if ((where + rLen < 0) || (where >= len))
  276.     {
  277.         return string;
  278.     }
  279.    
  280.     for (i = 0; i < len; i++)
  281.     {
  282.         if (inRange(where, where+rLen, i))
  283.         {
  284.             ret = StrParam(s:ret, c:GetChar(repl, i-where));
  285.         }
  286.         else
  287.         {
  288.             ret = StrParam(s:ret, c:GetChar(string, i));
  289.         }
  290.     }
  291.  
  292.     return ret;
  293. }
  294.  
  295. function int sliceString(int string, int start, int end)
  296. {
  297.     int len = StrLen(string);
  298.     int ret = "";
  299.     int i;
  300.  
  301.     if (start < 0)
  302.     {
  303.         start = len + start;
  304.     }
  305.  
  306.     if (end <= 0)
  307.     {
  308.         end = len + end;
  309.     }
  310.  
  311.     start = max(0, start);
  312.     end   = min(end, len-1);
  313.    
  314.     for (i = start; i < end; i++)
  315.     {
  316.         ret = StrParam(s:ret, c:GetChar(string, i));
  317.     }
  318.  
  319.     return ret;
  320. }
  321.  
  322. // End StrParam
  323.  
  324. function int unusedTID(int start, int end)
  325. {
  326.     int ret = start - 1;
  327.     int tidNum;
  328.  
  329.     if (start > end)
  330.     {
  331.         return -127;
  332.     }
  333.    
  334.     while (ret++ != end)
  335.     {
  336.         if (ThingCount(0, ret) == 0)
  337.         {
  338.             return ret;
  339.         }
  340.     }
  341.    
  342.     return -1;
  343. }
  344.  
  345. function int getMaxHealth(void)
  346. {
  347.     int maxHP = GetActorProperty(0, APROP_SpawnHealth);
  348.  
  349.     if ((maxHP == 0) && (PlayerNumber() != -1))
  350.     {
  351.         maxHP = 100;
  352.     }
  353.  
  354.     return maxHP;
  355. }
  356.  
  357. function void giveHealth(int amount)
  358. {
  359.     giveHealthFactor(amount, 1.0);
  360. }
  361.  
  362. function void giveHealthFactor(int amount, int maxFactor)
  363. {
  364.     int maxHP = ftoi(getMaxHealth() * maxFactor);
  365.     int newHP;
  366.  
  367.     int curHP = GetActorProperty(0, APROP_Health);
  368.  
  369.     if (maxFactor == 0.0) { newHP = max(curHP, curHP+amount); }
  370.     else { newHP = middle(curHP, curHP+amount, maxHP); }
  371.  
  372.     SetActorProperty(0, APROP_Health, newHP);
  373. }
  374.  
  375. function int isFreeForAll(void)
  376. {
  377.     if (GetCVar("terminator") || GetCVar("duel"))
  378.     {
  379.         return 1;
  380.     }
  381.  
  382.     int check1 = GetCVar("deathmatch") || GetCVar("possession") || GetCVar("lastmanstanding");
  383.     int check2 = check1 && !GetCVar("teamplay");
  384.  
  385.     return check2;
  386. }
  387.  
  388. function int isTeamGame(void)
  389. {
  390.     int ret = (GetCVar("teamplay") || GetCVar("teamgame"));
  391.     return ret;
  392. }
  393.  
  394. function int spawnDistance(int item, int dist, int tid)
  395. {
  396.     int myX, myY, myZ, myAng, myPitch, spawnX, spawnY, spawnZ;
  397.  
  398.     myX = GetActorX(0); myY = GetActorY(0); myZ = GetActorZ(0);
  399.     myAng = GetActorAngle(0); myPitch = GetActorPitch(0);
  400.  
  401.     spawnX = FixedMul(cos(myAng) * dist, cos(myPitch));
  402.     spawnX += myX;
  403.     spawnY = FixedMul(sin(myAng) * dist, cos(myPitch));
  404.     spawnY += myY;
  405.     spawnZ = myZ + (-sin(myPitch) * dist);
  406.  
  407.     return Spawn(item, spawnX, spawnY, spawnZ, tid, myAng >> 8);
  408. }
  409.  
  410. function void GiveAmmo(int type, int amount)
  411. {
  412.     if (GetCVar("sv_doubleammo"))
  413.     {
  414.         int m = GetAmmoCapacity(type);
  415.         int expected = min(m, CheckInventory(type) + amount);
  416.  
  417.         GiveInventory(type, amount);
  418.         TakeInventory(type, CheckInventory(type) - expected);
  419.     }
  420.     else
  421.     {  
  422.         GiveInventory(type, amount);
  423.     }
  424. }
  425.  
  426. function void GiveActorAmmo(int tid, int type, int amount)
  427. {
  428.     if (GetCVar("sv_doubleammo"))
  429.     {
  430.         int m = GetAmmoCapacity(type);
  431.         int expected = min(m, CheckActorInventory(tid, type) + amount);
  432.  
  433.         GiveActorInventory(tid, type, amount);
  434.         TakeActorInventory(tid, type, CheckActorInventory(tid, type) - expected);
  435.     }
  436.     else
  437.     {  
  438.         GiveActorInventory(tid, type, amount);
  439.     }
  440. }
  441.  
  442. function int cond(int test, int trueRet, int falseRet)
  443. {
  444.     if (test) { return trueRet; }
  445.     return falseRet;
  446. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement