Advertisement
ijontichy

commonFuncs.h

Sep 29th, 2013
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 21.49 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 TEAMCOUNT 8
  6. #define DEFAULTTID_SCRIPT 471
  7.  
  8. #define SECOND_TICS 35.714285714285715
  9. #define UNIT_CM     2.73921568627451
  10.  
  11. #define DAMAGE_NORANDOM     0x40000000
  12.  
  13. int TeamNames[TEAMCOUNT] =
  14. {
  15.     "Blue", "Red", "Green", "Gold", "Black", "White", "Orange", "Purple"
  16. };
  17.  
  18. int TeamColors[TEAMCOUNT] =
  19. {
  20.     CR_BLUE, CR_RED, CR_GREEN, CR_GOLD, CR_BLACK, CR_WHITE, CR_ORANGE, CR_PURPLE
  21. };
  22.  
  23. int TeamColorCodes[TEAMCOUNT] =
  24. {
  25.     "\ch", "\cg", "\cd", "\cf", "\cm", "\cj", "\ci", "\ct"
  26. };
  27.  
  28. function int itof(int x) { return x << 16; }
  29. function int ftoi(int x) { return x >> 16; }
  30.  
  31. function int abs(int x)
  32. {
  33.     if (x < 0) { return -x; }
  34.     return x;
  35. }
  36.  
  37. function int sign(int x)
  38. {
  39.     if (x < 0) { return -1; }
  40.     return 1;
  41. }
  42.  
  43. function int randSign(void)
  44. {
  45.     return (2*random(0,1))-1;
  46. }
  47.  
  48. function int mod(int x, int y)
  49. {
  50.     int ret = x - ((x / y) * y);
  51.     if (ret < 0) { ret = y + ret; }
  52.     return ret;
  53. }
  54.  
  55. function int pow(int x, int y)
  56. {
  57.     int n = 1;
  58.     while (y-- > 0) { n *= x; }
  59.     return n;
  60. }
  61.  
  62. function int powFloat(int x, int y)
  63. {
  64.     int n = 1.0;
  65.     while (y-- > 0) { n = FixedMul(n, x); }
  66.     return n;
  67. }
  68.  
  69. function int gcf(int a, int b)
  70. {
  71.     int c;
  72.     while (1)
  73.     {
  74.         if (b == 0) { return a; }
  75.         c = a % b;
  76.         a = b;
  77.         b = c;
  78.     }
  79.    
  80.     return -1;
  81. }
  82.  
  83. function int min(int x, int y)
  84. {
  85.     if (x < y) { return x; }
  86.     return y;
  87. }
  88.  
  89. function int max(int x, int y)
  90. {
  91.     if (x > y) { return x; }
  92.     return y;
  93. }
  94.  
  95. function int middle(int x, int y, int z)
  96. {
  97.     if ((x < z) && (y < z)) { return max(x, y); }
  98.     return max(min(x, y), z);
  99. }
  100.  
  101. function int percFloat(int intg, int frac)
  102. {
  103.     return itof(intg) + (itof(frac) / 100);
  104. }
  105.  
  106. function int percFloat2(int intg, int frac1, int frac2)
  107. {
  108.     return itof(intg) + (itof(frac1) / 100) + (itof(frac2) / 10000);
  109. }
  110.  
  111. function int keyUp(int key)
  112. {
  113.     int buttons = GetPlayerInput(-1, INPUT_BUTTONS);
  114.  
  115.     if ((~buttons & key) == key) { return 1; }
  116.     return 0;
  117. }
  118.  
  119. function int keyUp_any(int key)
  120. {
  121.     int buttons = GetPlayerInput(-1, INPUT_BUTTONS);
  122.  
  123.     if (~buttons & key) { return 1; }
  124.     return 0;
  125. }
  126.  
  127. function int keyDown(int key)
  128. {
  129.     int buttons = GetPlayerInput(-1, INPUT_BUTTONS);
  130.  
  131.     if ((buttons & key) == key) { return 1; }
  132.     return 0;
  133. }
  134.  
  135. function int keyDown_any(int key)
  136. {
  137.     int buttons = GetPlayerInput(-1, INPUT_BUTTONS);
  138.  
  139.     if (buttons & key) { return 1; }
  140.     return 0;
  141. }
  142.  
  143. function int keysPressed(void)
  144. {
  145.     int buttons     = GetPlayerInput(-1, INPUT_BUTTONS);
  146.     int oldbuttons  = GetPlayerInput(-1, INPUT_OLDBUTTONS);
  147.     int newbuttons  = (buttons ^ oldbuttons) & buttons;
  148.  
  149.     return newbuttons;
  150. }
  151.  
  152. function int keyPressed(int key)
  153. {
  154.     if ((keysPressed() & key) == key) { return 1; }
  155.     return 0;
  156. }
  157.  
  158. function int keyPressed_any(int key)
  159. {
  160.     if (keysPressed() & key) { return 1; }
  161.     return 0;
  162. }
  163.  
  164. function int inputUp(int input)
  165. {
  166.     int buttons = GetPlayerInput(-1, MODINPUT_BUTTONS);
  167.  
  168.     if ((~buttons & input) == input) { return 1; }
  169.     return 0;
  170. }
  171.  
  172. function int inputUp_any(int input)
  173. {
  174.     int buttons = GetPlayerInput(-1, MODINPUT_BUTTONS);
  175.  
  176.     if (~buttons & input) { return 1; }
  177.     return 0;
  178. }
  179.  
  180. function int inputDown(int input)
  181. {
  182.     int buttons = GetPlayerInput(-1, MODINPUT_BUTTONS);
  183.  
  184.     if ((buttons & input) == input) { return 1; }
  185.     return 0;
  186. }
  187.  
  188. function int inputDown_any(int input)
  189. {
  190.     int buttons = GetPlayerInput(-1, MODINPUT_BUTTONS);
  191.  
  192.     if (buttons & input) { return 1; }
  193.     return 0;
  194. }
  195.  
  196. function int inputsPressed(void)
  197. {
  198.     int buttons     = GetPlayerInput(-1, MODINPUT_BUTTONS);
  199.     int oldbuttons  = GetPlayerInput(-1, MODINPUT_OLDBUTTONS);
  200.     int newbuttons  = (buttons ^ oldbuttons) & buttons;
  201.  
  202.     return newbuttons;
  203. }
  204.  
  205. function int inputPressed(int input)
  206. {
  207.     if ((inputsPressed() & input) == input) { return 1; }
  208.     return 0;
  209. }
  210.  
  211. function int inputPressed_any(int input)
  212. {
  213.     if (inputsPressed() & input) { return 1; }
  214.     return 0;
  215. }
  216.  
  217. function int adjustBottom(int tmin, int tmax, int i)
  218. {
  219.     if (tmin > tmax)
  220.     {
  221.         tmax ^= tmin; tmin ^= tmax; tmax ^= tmin;  // XOR swap
  222.     }
  223.  
  224.     if (i < tmin) { tmin = i; }
  225.     if (i > tmax) { tmin += (i - tmax); }
  226.  
  227.     return tmin;
  228. }
  229.  
  230. function int adjustTop(int tmin, int tmax, int i)
  231. {
  232.     if (tmin > tmax)
  233.     {
  234.         tmax ^= tmin; tmin ^= tmax; tmax ^= tmin;
  235.     }
  236.  
  237.     if (i < tmin) { tmax -= (tmin - i); }
  238.     if (i > tmax) { tmax = i; }
  239.  
  240.     return tmax;
  241. }
  242.  
  243. function int adjustShort(int tmin, int tmax, int i)
  244. {
  245.     if (tmin > tmax)
  246.     {
  247.         tmax ^= tmin; tmin ^= tmax; tmax ^= tmin;
  248.     }
  249.  
  250.     if (i < tmin)
  251.     {
  252.         tmax -= (tmin - i);
  253.         tmin = i;
  254.     }
  255.     if (i > tmax)
  256.     {
  257.         tmin += (i - tmax);
  258.         tmax = i;
  259.     }
  260.    
  261.     return packShorts(tmin, tmax);
  262. }
  263.  
  264.  
  265. // Taken from http://zdoom.org/wiki/sqrt
  266.  
  267. function int sqrt_i(int number)
  268. {
  269.     if (number <= 3) { return number > 0; }
  270.  
  271.     int oldAns = number >> 1,                     // initial guess
  272.         newAns = (oldAns + (number / oldAns)) >> 1; // first iteration
  273.  
  274.     // main iterative method
  275.     while (newAns < oldAns)
  276.     {
  277.         oldAns = newAns;
  278.         newAns = (oldAns + number / oldAns) >> 1;
  279.     }
  280.  
  281.     return oldAns;
  282. }
  283.  
  284. function int sqrt(int number)
  285. {
  286.     if (number == 1.0) { return 1.0; }
  287.     if (number <= 0) { return 0; }
  288.     int val = 150.0;
  289.     for (int i=0; i<15; i++) { val = (val + FixedDiv(number, val)) >> 1; }
  290.  
  291.     return val;
  292. }
  293.  
  294. function int magnitudeTwo(int x, int y)
  295. {
  296.     return sqrt_i(x*x + y*y);
  297. }
  298.  
  299. function int magnitudeTwo_f(int x, int y)
  300. {
  301.     int len, ang;
  302.  
  303.     ang = VectorAngle(x, y);
  304.     if (((ang + 0.125) % 0.5) > 0.25) { len = FixedDiv(y, sin(ang)); }
  305.     else { len = FixedDiv(x, cos(ang)); }
  306.  
  307.     return len;
  308. }
  309.  
  310. function int magnitudeThree(int x, int y, int z)
  311. {
  312.     return sqrt_i(x*x + y*y + z*z);
  313. }
  314.  
  315. function int magnitudeThree_f(int x, int y, int z)
  316. {
  317.     int len, ang;
  318.  
  319.     ang = VectorAngle(x, y);
  320.     if (((ang + 0.125) % 0.5) > 0.25) { len = FixedDiv(y, sin(ang)); }
  321.     else { len = FixedDiv(x, cos(ang)); }
  322.  
  323.     ang = VectorAngle(len, z);
  324.     if (((ang + 0.125) % 0.5) > 0.25) { len = FixedDiv(z, sin(ang)); }
  325.     else { len = FixedDiv(len, cos(ang)); }
  326.  
  327.     return len;
  328. }
  329.  
  330.  
  331. function int quadPos(int a, int b, int c)
  332. {
  333.     int s1 = sqrt(FixedMul(b, b)-(4*FixedMul(a, c)));
  334.     int s2 = (2 * a);
  335.     int b1 = FixedDiv(-b + s1, s2);
  336.  
  337.     return b1;
  338. }
  339.  
  340. function int quadNeg(int a, int b, int c)
  341. {
  342.     int s1 = sqrt(FixedMul(b, b)-(4*FixedMul(a, c)));
  343.     int s2 = (2 * a);
  344.     int b1 = FixedDiv(-b - s1, s2);
  345.  
  346.     return b1;
  347. }
  348.  
  349. // All the arguments are to be fixed-point
  350. function int quad(int a, int b, int c, int y)
  351. {
  352.     return FixedMul(a, FixedMul(y, y)) + FixedMul(b, y) + c + y;
  353. }
  354.  
  355. function int quadHigh(int a, int b, int c, int x)
  356. {
  357.     return quadPos(a, b, c-x);
  358. }
  359.  
  360. function int quadLow(int a, int b, int c, int x)
  361. {
  362.     return quadNeg(a, b, c-x);
  363. }
  364.  
  365. function int inRange(int low, int high, int x)
  366. {
  367.     return ((x >= low) && (x < high));
  368. }
  369.  
  370. function void AddAmmoCapacity(int type, int add)
  371. {
  372.     SetAmmoCapacity(type, GetAmmoCapacity(type) + add);
  373. }
  374.  
  375. function int packShorts(int left, int right)
  376. {
  377.     return ((left & 0xFFFF) << 16) + (right & 0xFFFF);
  378. }
  379.  
  380. function int leftShort(int packed) { return packed >> 16; }
  381. function int rightShort(int packed) { return (packed << 16) >> 16; }
  382.  
  383.  
  384. // This stuff only works with StrParam
  385.  
  386. function int cleanString(int string)
  387. {
  388.     int ret = "";
  389.     int strSize = StrLen(string);
  390.  
  391.     int c, i, ignoreNext;
  392.    
  393.     for (i = 0; i < strSize; i++)
  394.     {
  395.         c = GetChar(string, i);
  396.  
  397.         if ( ( ((c > 8) && (c < 14)) || ((c > 31) && (c < 127)) || ((c > 160) && (c < 173)) ) && !ignoreNext)
  398.         {
  399.             ret = StrParam(s:ret, c:c);
  400.         }
  401.         else if (c == 28 && !ignoreNext)
  402.         {
  403.             ignoreNext = 1;
  404.         }
  405.         else
  406.         {
  407.             ignoreNext = 0;
  408.         }
  409.     }
  410.  
  411.     return ret;
  412. }
  413.  
  414. function int cvarFromString(int prefix, int newname)
  415. {
  416.     int ret = "";
  417.     int i, c;
  418.     int prelen = strlen(prefix);
  419.     int namelen = strlen(newname);
  420.     int cap = prelen+namelen;
  421.  
  422.     for (i = 0; i <= cap; i++)
  423.     {
  424.         c = cond(i >= prelen, GetChar(newname, i-prelen), GetChar(prefix, i));
  425.  
  426.         if (
  427.             (c > 64 && c < 91)  // is uppercase letter
  428.          || (c > 90 && c < 123) // is lowercase letter
  429.          || (c > 47 && c < 58)  // is number
  430.          || c == 95             // _
  431.          )
  432.         {
  433.             ret = StrParam(s:ret, c:c);
  434.         }
  435.     }
  436.  
  437.     return ret;
  438. }
  439.  
  440. function int padStringR(int baseStr, int padChar, int len)
  441. {
  442.     int baseStrLen = StrLen(baseStr);
  443.     int pad = "";
  444.     int padLen; int i;
  445.  
  446.     if (baseStrLen >= len)
  447.     {
  448.         return baseStr;
  449.     }
  450.    
  451.     padChar = GetChar(padChar, 0);
  452.     padLen = len - baseStrLen;
  453.  
  454.     for (i = 0; i < padLen; i++)
  455.     {
  456.         pad = StrParam(s:pad, c:padChar);
  457.     }
  458.  
  459.     return StrParam(s:baseStr, s:pad);
  460. }
  461.  
  462. function int padStringL(int baseStr, int padChar, int len)
  463. {
  464.     int baseStrLen = StrLen(baseStr);
  465.     int pad = "";
  466.     int padLen; int i;
  467.  
  468.     if (baseStrLen >= len)
  469.     {
  470.         return baseStr;
  471.     }
  472.    
  473.     padChar = GetChar(padChar, 0);
  474.     padLen = len - baseStrLen;
  475.  
  476.     for (i = 0; i < padLen; i++)
  477.     {
  478.         pad = StrParam(s:pad, c:padChar);
  479.     }
  480.  
  481.     return StrParam(s:pad, s:baseStr);
  482. }
  483.  
  484. function int changeString(int string, int repl, int where)
  485. {
  486.     int i; int j; int k;
  487.     int ret = "";
  488.     int len = StrLen(string);
  489.     int rLen = StrLen(repl);
  490.  
  491.     if ((where + rLen < 0) || (where >= len))
  492.     {
  493.         return string;
  494.     }
  495.    
  496.     for (i = 0; i < len; i++)
  497.     {
  498.         if (inRange(where, where+rLen, i))
  499.         {
  500.             ret = StrParam(s:ret, c:GetChar(repl, i-where));
  501.         }
  502.         else
  503.         {
  504.             ret = StrParam(s:ret, c:GetChar(string, i));
  505.         }
  506.     }
  507.  
  508.     return ret;
  509. }
  510.  
  511. function int sliceString(int string, int start, int end)
  512. {
  513.     int len = StrLen(string);
  514.     int ret = "";
  515.     int i;
  516.  
  517.     if (start < 0)
  518.     {
  519.         start = len + start;
  520.     }
  521.  
  522.     if (end <= 0)
  523.     {
  524.         end = len + end;
  525.     }
  526.  
  527.     start = max(0, start);
  528.     end   = min(end, len-1);
  529.    
  530.     for (i = start; i < end; i++)
  531.     {
  532.         ret = StrParam(s:ret, c:GetChar(string, i));
  533.     }
  534.  
  535.     return ret;
  536. }
  537.  
  538. function int strcmp(int str1, int str2)
  539. {
  540.     int i,j,k,l;
  541.     int len1 = StrLen(str1);
  542.     int len2 = StrLen(str2);
  543.     j = max(len1, len2);
  544.  
  545.     for (i = 0; i < j; i++)
  546.     {
  547.         if (i >= len1) { return -1; }
  548.         if (i >= len2) { return  1; }
  549.        
  550.         k = GetChar(str1, i); l = GetChar(str2, i);
  551.  
  552.         if (k > j) { return  1; }
  553.         if (k < j) { return -1; }
  554.     }
  555.     return 0;
  556. }
  557.  
  558.  
  559. // End StrParam
  560.  
  561. function int unusedTID(int start, int end)
  562. {
  563.     int ret = start - 1;
  564.     int tidNum;
  565.  
  566.     if (start > end) { start ^= end; end ^= start; start ^= end; }  // good ol' XOR swap
  567.    
  568.     while (ret++ != end)
  569.     {
  570.         if (ThingCount(0, ret) == 0)
  571.         {
  572.             return ret;
  573.         }
  574.     }
  575.    
  576.     return -1;
  577. }
  578.  
  579. function int getMaxHealth(void)
  580. {
  581.     int maxHP = GetActorProperty(0, APROP_SpawnHealth);
  582.  
  583.     if ((maxHP == 0) && (PlayerNumber() != -1))
  584.     {
  585.         maxHP = 100;
  586.     }
  587.  
  588.     return maxHP;
  589. }
  590.  
  591. function int giveHealth(int amount)
  592. {
  593.     return giveHealthFactor(amount, 1.0);
  594. }
  595.  
  596. function int giveHealthFactor(int amount, int maxFactor)
  597. {
  598.     return giveHealthMax(amount, FixedMul(getMaxHealth(), maxFactor));
  599. }
  600.  
  601. function int giveHealthMax(int amount, int maxHP)
  602. {
  603.     int newHP;
  604.  
  605.     int curHP = GetActorProperty(0, APROP_Health);
  606.  
  607.     if (maxHP == 0) { newHP = max(curHP, curHP+amount); }
  608.     else
  609.     {
  610.         if (curHP > maxHP) { return 0; }
  611.         newHP = middle(curHP, curHP+amount, maxHP);
  612.     }
  613.  
  614.     SetActorProperty(0, APROP_Health, newHP);
  615.  
  616.     return newHP - curHP;
  617. }
  618.  
  619. function int isDead(int tid)
  620. {
  621.     return GetActorProperty(tid, APROP_Health) <= 0;
  622. }
  623.  
  624. function int isSinglePlayer(void)
  625. {
  626.     return GameType() == GAME_SINGLE_PLAYER;
  627. }
  628.  
  629. function int isLMS(void)
  630. {
  631.     return GetCVar("lastmanstanding") || GetCVar("teamlms");
  632. }
  633.  
  634. function int isCoop(void)
  635. {
  636.     int check1 = GameType() == GAME_NET_COOPERATIVE;
  637.     int check2 = GetCVar("cooperative") || GetCVar("invasion") || GetCVar("survival");
  638.  
  639.     return check1 || check2;
  640. }
  641.  
  642. function int isInvasion(void)
  643. {
  644.     return GetCVar("invasion");
  645. }
  646.  
  647. function int isFreeForAll(void)
  648. {
  649.     if (GetCVar("terminator") || GetCVar("duel"))
  650.     {
  651.         return 1;
  652.     }
  653.  
  654.     int check1 = GetCVar("deathmatch") || GetCVar("possession") || GetCVar("lastmanstanding");
  655.     int check2 = check1 && !GetCVar("teamplay");
  656.  
  657.     return check2;
  658. }
  659.  
  660. function int isTeamGame(void)
  661. {
  662.     int ret = (GetCVar("teamplay") || GetCVar("teamgame") || GetCVar("teamlms"));
  663.     return ret;
  664. }
  665.  
  666. function int spawnDistance(int item, int dist, int tid)
  667. {
  668.     int myX, myY, myZ, myAng, myPitch, spawnX, spawnY, spawnZ;
  669.  
  670.     myX = GetActorX(0); myY = GetActorY(0); myZ = GetActorZ(0);
  671.     myAng = GetActorAngle(0); myPitch = GetActorPitch(0);
  672.  
  673.     spawnX = FixedMul(cos(myAng) * dist, cos(myPitch));
  674.     spawnX += myX;
  675.     spawnY = FixedMul(sin(myAng) * dist, cos(myPitch));
  676.     spawnY += myY;
  677.     spawnZ = myZ + (-sin(myPitch) * dist);
  678.  
  679.     return Spawn(item, spawnX, spawnY, spawnZ, tid, myAng >> 8);
  680. }
  681.  
  682. function void SetInventory(int item, int amount)
  683. {
  684.     int count = CheckInventory(item);
  685.  
  686.     if (count == amount) { return; }
  687.    
  688.     if (count > amount)
  689.     {
  690.         TakeInventory(item, count - amount);
  691.         return;
  692.     }
  693.  
  694.     GiveAmmo(item, amount - count);
  695.     return;
  696. }
  697.  
  698. function int ToggleInventory(int inv)
  699. {
  700.     if (CheckInventory(inv))
  701.     {
  702.         TakeInventory(inv, 0x7FFFFFFF);
  703.         return 0;
  704.     }
  705.  
  706.     GiveInventory(inv, 1);
  707.     return 1;
  708. }
  709.  
  710. function void GiveAmmo(int type, int amount)
  711. {
  712.     if (GetCVar("sv_doubleammo"))
  713.     {
  714.         int m = GetAmmoCapacity(type);
  715.         int expected = min(m, CheckInventory(type) + amount);
  716.  
  717.         GiveInventory(type, amount);
  718.         TakeInventory(type, CheckInventory(type) - expected);
  719.     }
  720.     else
  721.     {  
  722.         GiveInventory(type, amount);
  723.     }
  724. }
  725.  
  726. function void GiveActorAmmo(int tid, int type, int amount)
  727. {
  728.     if (GetCVar("sv_doubleammo"))
  729.     {
  730.         int m = GetAmmoCapacity(type);
  731.         int expected = min(m, CheckActorInventory(tid, type) + amount);
  732.  
  733.         GiveActorInventory(tid, type, amount);
  734.         TakeActorInventory(tid, type, CheckActorInventory(tid, type) - expected);
  735.     }
  736.     else
  737.     {  
  738.         GiveActorInventory(tid, type, amount);
  739.     }
  740. }
  741.  
  742. function int cond(int test, int trueRet, int falseRet)
  743. {
  744.     if (test) { return trueRet; }
  745.     return falseRet;
  746. }
  747.  
  748. function int condTrue(int test, int trueRet)
  749. {
  750.     if (test) { return trueRet; }
  751.     return test;
  752. }
  753.  
  754. function int condFalse(int test, int falseRet)
  755. {
  756.     if (test) { return test; }
  757.     return falseRet;
  758. }
  759.  
  760. function void saveCVar(int cvar, int val)
  761. {
  762.     int setStr = StrParam(s:"set ", s:cvar, s:" ", d:val);
  763.     int arcStr = StrParam(s:"archivecvar ", s:cvar);
  764.     ConsoleCommand(setStr); ConsoleCommand(arcStr);
  765. }
  766.  
  767. function int defaultCVar(int cvar, int defaultVal)
  768. {
  769.     int ret = GetCVar(cvar);
  770.     if (ret == 0) { saveCVar(cvar, defaultVal); return defaultVal; }
  771.  
  772.     return ret;
  773. }
  774.  
  775.  
  776. function int onGround(int tid)
  777. {
  778.     return (GetActorZ(tid) - GetActorFloorZ(tid)) == 0;
  779. }
  780.  
  781. function int ThingCounts(int start, int end)
  782. {
  783.     int i, ret = 0;
  784.  
  785.     if (start > end) { start ^= end; end ^= start; start ^= end; }
  786.     for (i = start; i < end; i++) { ret += ThingCount(0, i); }
  787.  
  788.     return ret;
  789. }
  790.  
  791. function int PlaceOnFloor(int tid)
  792. {
  793.     if (ThingCount(0, tid) != 1) { return 1; }
  794.    
  795.     SetActorPosition(tid, GetActorX(tid), GetActorY(tid), GetActorFloorZ(tid), 0);
  796.     return 0;
  797. }
  798.  
  799. #define DIR_E  1
  800. #define DIR_NE 2
  801. #define DIR_N  3
  802. #define DIR_NW 4
  803. #define DIR_W  5
  804. #define DIR_SW 6
  805. #define DIR_S  7
  806. #define DIR_SE 8
  807.  
  808. function int getDirection(void)
  809. {
  810.     int sideMove = keyDown(BT_MOVERIGHT) - keyDown(BT_MOVELEFT);
  811.     int forwMove = keyDown(BT_FORWARD) - keyDown(BT_BACK);
  812.  
  813.     if (sideMove || forwMove)
  814.     {
  815.         switch (sideMove)
  816.         {
  817.           case -1:
  818.             switch (forwMove)
  819.             {
  820.                 case -1: return DIR_SW;
  821.                 case  0: return DIR_W;
  822.                 case  1: return DIR_NW;
  823.             }
  824.             break;
  825.  
  826.           case 0:
  827.             switch (forwMove)
  828.             {
  829.                 case -1: return DIR_S;
  830.                 case  1: return DIR_N;
  831.             }
  832.             break;
  833.  
  834.           case 1:
  835.             switch (forwMove)
  836.             {
  837.                 case -1: return DIR_SE;
  838.                 case  0: return DIR_E;
  839.                 case  1: return DIR_NE;
  840.             }
  841.             break;
  842.         }
  843.     }
  844.  
  845.     return 0;
  846. }
  847.  
  848. function int isInvulnerable(void)
  849. {
  850.     int check1 = GetActorProperty(0, APROP_Invulnerable);
  851.     int check2 = CheckInventory("PowerInvulnerable");
  852.  
  853.     return check1 || check2;
  854. }
  855.  
  856. function void saveStringCVar(int string, int cvarname)
  857. {
  858.     int slen = StrLen(string);
  859.     int i, c, cvarname2;
  860.  
  861.     for (i = 0; i < slen; i++)
  862.     {
  863.         cvarname2 = StrParam(s:cvarname, s:"_char", d:i);
  864.         SaveCVar(cvarname2, GetChar(string, i));
  865.     }
  866.  
  867.     while (1)
  868.     {
  869.         cvarname2 = StrParam(s:cvarname, s:"_char", d:i);
  870.         c = GetCVar(cvarname2);
  871.  
  872.         if (c == 0) { break; }
  873.  
  874.         ConsoleCommand(StrParam(s:"unset ", s:cvarname2));
  875.         i += 1;
  876.     }
  877. }
  878.  
  879. function int loadStringCVar(int cvarname)
  880. {
  881.     int ret = "";
  882.     int i = 0, c, cvarname2;
  883.  
  884.     while (1)
  885.     {
  886.         cvarname2 = StrParam(s:cvarname, s:"_char", d:i);
  887.         c = GetCVar(cvarname2);
  888.  
  889.         if (c == 0) { break; }
  890.  
  891.         ret = StrParam(s:ret, c:c);
  892.         i += 1;
  893.     }
  894.  
  895.     return ret;
  896. }
  897.  
  898. function int defaultTID(int def)
  899. {
  900.     return _defaulttid(def, 0);
  901. }
  902.  
  903. function int _defaulttid(int def, int alwaysPropagate)
  904. {
  905.     if (ClassifyActor(0) & ACTOR_WORLD) { return 0; }
  906.  
  907.     int tid = ActivatorTID();
  908.     int i, changed = 0;
  909.  
  910.     if (ThingCount(0, tid) != 1)
  911.     {
  912.         tid = def;
  913.         changed = 1;
  914.         if (def <= 0)
  915.         {
  916.             i = random(12, 220);
  917.             tid = unusedTID(i*100, (i+100)*100);
  918.         }
  919.  
  920.         Thing_ChangeTID(0, tid);
  921.     }
  922.  
  923.     if ((changed || (alwaysPropagate == 1)) && (alwaysPropagate != 2))
  924.     {
  925.         ACS_ExecuteAlways(DEFAULTTID_SCRIPT, 0, tid,0,0);
  926.     }
  927.  
  928.     return tid;
  929. }
  930.  
  931. script DEFAULTTID_SCRIPT (int tid) clientside
  932. {
  933.     if (ConsolePlayerNumber() == -1) { terminate; }
  934.     Thing_ChangeTID(0, tid);
  935. }
  936.  
  937. function int JumpZFromHeight(int height, int gravFactor)
  938. {
  939.     return sqrt(2 * height * gravFactor);
  940. }
  941.  
  942. function int roundZero(int toround)
  943. {
  944.     int i = toround % 1.0;
  945.     return ftoi(toround - i);
  946. }
  947.  
  948. function int roundAway(int toround)
  949. {
  950.     int i = toround % 1.0;
  951.  
  952.     if (i == 0) { return ftoi(toround); }
  953.     return ftoi(toround + (1.0 - i));
  954. }
  955.  
  956. function int round(int toround)
  957. {
  958.     return ftoi(toround + 0.5);
  959. }
  960.  
  961. function int ceil(int toround)
  962. {
  963.     return ftoi(toround + (1.0-1));
  964. }
  965.  
  966. function int intFloat(int toround)
  967. {
  968.     return itof(ftoi(toround));
  969. }
  970.  
  971. function int distance(int x1, int y1, int z1, int x2, int y2, int z2)
  972. {
  973.     return magnitudeThree_f(x2-x1, y2-y1, z2-z1);
  974. }
  975.  
  976. function int distance_tid(int tid1, int tid2)
  977. {
  978.     int x1 = GetActorX(tid1);
  979.     int y1 = GetActorY(tid1);
  980.     int z1 = GetActorZ(tid1);
  981.  
  982.     int x2 = GetActorX(tid2);
  983.     int y2 = GetActorY(tid2);
  984.     int z2 = GetActorZ(tid2);
  985.  
  986.     return magnitudeThree_f(x2-x1, y2-y1, z2-z1);
  987. }
  988.  
  989. function int distance_ftoi(int x1, int y1, int z1, int x2, int y2, int z2)
  990. {
  991.     return ftoi(distance(x1,y1,z1, x2,y2,z2));
  992. }
  993.  
  994. function void printDebugInfo(void)
  995. {
  996.     int classify    = ClassifyActor(0);
  997.     int fead        = classify & ACTOR_DEAD;
  998.     int player      = classify & ACTOR_PLAYER;
  999.     int pln         = PlayerNumber();
  1000.  
  1001.     Log(s:" -- DEBUG INFO -- ");
  1002.  
  1003.     Log(s:"Executed on tic ", d:Timer(), s:" on map ", d:GetLevelInfo(LEVELINFO_LEVELNUM));
  1004.  
  1005.     if (classify & (ACTOR_PLAYER | ACTOR_MONSTER))
  1006.     {
  1007.         Log(s:"Script activator has ", d:GetActorProperty(0, APROP_Health), s:"/", d:getMaxHealth(), s:" HP");
  1008.     }
  1009.  
  1010.     if (player)
  1011.     {
  1012.         Log(s:"Is player ", d:pln, s:" (", n:0, s:"\c-) with class number ", d:PlayerClass(pln));
  1013.     }
  1014.  
  1015.     Log(s:" -- END DEBUG -- ");
  1016. }
  1017.  
  1018.  
  1019. function int PlayerTeamCount(int teamNo)
  1020. {
  1021.     int i, ret;
  1022.     for (i = 0; i < PLAYERMAX; i++)
  1023.     {
  1024.         if (GetPlayerInfo(i, PLAYERINFO_TEAM) == teamNO) { ret++; }
  1025.     }
  1026.     return ret;
  1027. }
  1028.  
  1029. function int lower(int chr)
  1030. {
  1031.     if (chr > 64 && chr < 91) { return chr+32; }
  1032.     return chr;
  1033. }
  1034.  
  1035. function int upper(int chr)
  1036. {
  1037.     if (chr > 90 && chr < 123) { return chr-32; }
  1038.     return chr;
  1039. }
  1040.  
  1041. function int AddActorProperty(int tid, int prop, int amount)
  1042. {
  1043.     int newAmount = GetActorProperty(tid, prop) + amount;
  1044.     SetActorProperty(tid, prop, newAmount);
  1045.     return newAmount;
  1046. }
  1047.  
  1048. function int ClientCount(void)
  1049. {
  1050.     int ret, i;
  1051.  
  1052.     for (i = 0; i < PLAYERMAX; i++)
  1053.     {
  1054.         if (PlayerInGame(i) || PlayerIsSpectator(i)) { ret++; }
  1055.     }
  1056.  
  1057.     return ret;
  1058. }
  1059.  
  1060. function int HasRoom(int actorname, int x, int y, int z)
  1061. {
  1062.     int tid = unusedTID(40000, 50000);
  1063.     int ret = Spawn(actorname, x, y, z, tid);
  1064.  
  1065.     if (ret >= 1) { Thing_Remove(tid); }
  1066.  
  1067.     return ret;
  1068. }
  1069.  
  1070. function int RealPlayerCount(void)
  1071. {
  1072.     int ret, i;
  1073.  
  1074.     for (i = 0; i < PLAYERMAX; i++)
  1075.     {
  1076.         if (PlayerInGame(i) && !PlayerIsBot(i)) { ret++; }
  1077.     }
  1078.  
  1079.     return ret;
  1080. }
  1081.  
  1082. function int quadSlope(int orgX, int orgY, int pntX, int pntY, int floatY)
  1083. {
  1084.     int dist = abs(pntX - orgX);
  1085.     int height = pntY - orgY;
  1086.     int negative = 0;
  1087.  
  1088.     if (height == 0) { return 0; }
  1089.  
  1090.     if (height < 0)
  1091.     {
  1092.         negative = -1;
  1093.         height = -height;
  1094.     }
  1095.    
  1096.     int slope = cond(floatY, sqrt(height), sqrt(itof(height)));
  1097.  
  1098.     slope = (slope / dist);
  1099.  
  1100.     slope = FixedMul(slope, slope);
  1101.  
  1102.     if (negative) { return -slope; }
  1103.     return slope;
  1104. }
  1105.  
  1106. function int actorVelMagnitude(int tid)
  1107. {
  1108.     return magnitudeThree_f(GetActorVelX(tid), GetActorVelY(tid), GetActorVelZ(tid));
  1109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement