Advertisement
ijontichy

ropeandrubber.c

Jul 30th, 2012
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 10.36 KB | None | 0 0
  1. #include "zcommon.acs"
  2. #library "ropeandrubber"
  3.  
  4. #include "ropeConstants.h"
  5. #include "ropeFunctions.h"
  6.  
  7.  
  8. /* -- script ROPE_OPEN [OPEN script] --
  9.  *   Checks if grapple_version is equal to 1, and sets grapple_version to 1,
  10.  * grapple_startwith to 1, and grapple_strength to 2 if it isn't.
  11.  */
  12.  
  13. script "rope_open" open
  14. {
  15.     if (GetCVar("grapple_version") != 1)
  16.     {
  17.         ConsoleCommand("set grapple_version  1");
  18.         ConsoleCommand("set grapple_startwith 1");
  19.         ConsoleCommand("set grapple_strength 2");
  20.         ConsoleCommand("archivecvar grapple_version");
  21.         ConsoleCommand("archivecvar grapple_startwith");
  22.         ConsoleCommand("archivecvar grapple_strength");
  23.     }
  24. }
  25.  
  26. /* -- script ROPE_ENTER [ENTER script] --
  27.  *   If grapple_startswith is 1, give you the grappling hook and grapple item.
  28.  */
  29.  
  30. script "rope_enter" enter
  31. {
  32.     if (GetCVar("grapple_startwith") == 1)
  33.     {
  34.         GiveInventory("Grappling Hook", 1);
  35.     }
  36. }
  37.  
  38. /* ** ROPE_HOLD (int ropeTID, int length, float strength)
  39.  *
  40.  * * Assumes that the object being roped is the activator of the script, due to
  41.  * * lack of parameters.
  42.  *
  43.  *
  44.  */
  45.  
  46. script "rope_hold" (int ropeTID, int length, int strength)
  47. {
  48.     int myX;   int myY;   int myZ;
  49.     int ropeX; int ropeY; int ropeZ;
  50.     int velX;  int velY;  int velZ;
  51.     int normX; int normY; int normZ;
  52.     int pullX; int pullY; int pullZ;
  53.     int opulX; int opulY; int opulZ;
  54.     int centX; int centY; int centZ;
  55.     int rPullX; int rPullY; int rPullZ;
  56.     int rOpulX; int rOpulY; int rOpulZ;
  57.     int mag; int cMag;
  58.     int mass1 = GetActorProperty(0, APROP_Mass) << 16;
  59.     int mass2 = GetActorProperty(ropeTID, APROP_Mass) << 16;
  60.     int massTotal = mass1 + mass2;
  61.  
  62.     int massF1; int massF2;
  63.     int pull1;  int pull2;
  64.  
  65.     int strict;
  66.  
  67.     int cLen = length >> 1;
  68.     int playerTID = ActivatorTID();
  69.  
  70.     if ((mass1 == 0) || (mass2 == -1.0))
  71.     {
  72.         massF1 = 1.0;
  73.         massF2 = 0.0;
  74.     }
  75.     else if ((mass2 == 0) || (mass1 == -1.0))
  76.     {
  77.         massF1 = 0.0;
  78.         massF2 = 1.0;
  79.     }
  80.     else
  81.     {
  82.         massF1 = FixedDiv(mass2, massTotal);
  83.         massF2 = FixedDiv(mass1, massTotal);
  84.     }
  85.  
  86.     Log(s:"mass1 is ", f:mass1, s:" ", d:mass1);
  87.     Log(s:"mass2 is ", f:mass2, s:" ", d:mass2);
  88.     Log(s:"massTotal is ", f:massTotal, s:" ", d:massTotal);
  89.     Log(s:"massF1 is ", f:massF1, s:" ", d:massF1);
  90.     Log(s:"massF2 is ", f:massF2, s:" ", d:massF2);
  91.  
  92.     int mode = (strength - 1) / 100;
  93.     strength %= 100;
  94.  
  95.     strength <<= 16;
  96.     strength /= 100;
  97.     strength = FixedMul(strength, strength);
  98.     Log(f:strength);
  99.    
  100.     if (length == -1)
  101.     {
  102.         myX   = GetActorX(0);    myY   = GetActorY(0);    myZ   = GetActorZ(0);
  103.         ropeX = GetActorX(ropeTID); ropeY = GetActorY(ropeTID); ropeZ = GetActorZ(ropeTID);
  104.         velX  = myX - ropeX;        velY  = myY - ropeY;        velZ  = myZ - ropeZ;
  105.  
  106.         length = magnitudeThree(velX >> 16, velY >> 16, velZ >> 16);
  107.     }
  108.  
  109.     if ((playerTID != 0) || (ThingCount(0, playerTID) != 1))
  110.     {
  111.         playerTID = unusedTID(5000, -1);
  112.         Thing_ChangeTID(0, playerTID);
  113.     }
  114.  
  115.     while (ThingCount(0, ropeTID) == 1)
  116.     {
  117.         strict = 0;
  118.  
  119.         myX   = GetActorX(0);    myY   = GetActorY(0);    myZ   = GetActorZ(0);
  120.         ropeX = GetActorX(ropeTID); ropeY = GetActorY(ropeTID); ropeZ = GetActorZ(ropeTID);
  121.         velX  = myX - ropeX;        velY  = myY - ropeY;        velZ  = myZ - ropeZ;
  122.  
  123.         // this is where precision goes down the drain
  124.         mag = magnitudeThree(velX >> 16, velY >> 16, velZ >> 16);
  125.    
  126.         if (mag != 0)
  127.         {
  128.             normX = velX / mag;   normY = velY / mag;   normZ = velZ / mag;
  129.         }
  130.         else
  131.         {
  132.             normX = 0; normY = 0; normZ = 0;
  133.         }
  134.  
  135.         switch (mode)
  136.         {
  137.  
  138.         case 2:
  139.             strict = (mag != length);
  140.         case 0:
  141.             if ((mag > length) || strict)
  142.             {
  143.                 pullX = -(velX - (normX * length));
  144.                 pullY = -(velY - (normY * length));
  145.                 pullZ = -(velZ - (normZ * length));
  146.  
  147.                 pullX = FixedMul(pullX, strength);
  148.                 pullY = FixedMul(pullY, strength);
  149.                 pullZ = FixedMul(pullZ, strength);
  150.  
  151.                 opulX = -FixedMul(pullX, massF2);
  152.                 opulY = -FixedMul(pullY, massF2);
  153.                 opulZ = -FixedMul(pullZ, massF2);
  154.  
  155.                 pullX = FixedMul(pullX, massF1);
  156.                 pullY = FixedMul(pullY, massF1);
  157.                 pullZ = FixedMul(pullZ, massF1);
  158.  
  159.                 SetActorVelocity(0, pullX, pullY, pullZ, 1, 0);
  160.                 SetActorVelocity(ropeTID, opulX, opulY, opulZ, 1, 0);
  161.             }
  162.             break;
  163.  
  164.         case 3:
  165.             strict = (mag != length);
  166.         case 1:
  167.             if ((mag > length) || strict)
  168.             {
  169.                 centX = ropeX + (velX >> 1);
  170.                 centY = ropeY + (velY >> 1);
  171.                 centZ = ropeZ + (velZ >> 1);
  172.  
  173.                 cMag  = mag >> 1;
  174.  
  175.                 pull1 = cMag - cLen;
  176.                 pull2 = pull1 * massF2;
  177.                 pull1 = pull1 * massF1;
  178.  
  179.                 pullX = -(velX - (centX * cLen));
  180.                 pullY = -(velY - (centY * cLen));
  181.                 pullZ = -(velZ - (centZ * cLen));
  182.             }
  183.             break;
  184.         }
  185.  
  186.         ACS_ExecuteAlways(ROPE_DRAWLINE, 0, ropeTID, playerTID, 24);
  187.         Delay(1);
  188.     }
  189.  
  190.     if (ThingCount(0, ropeTID) > 1)
  191.     {
  192.         Log(s:"Error: more than one object with tid ", d:ropeTID, s:" (is used for rope)");
  193.     }
  194. }
  195.  
  196. script ROPE_REEL (int grappleTid, int strength, int firetype)
  197. {
  198.     int nX;   int nY;   int nZ;
  199.     int plX;  int plY;  int plZ;
  200.     int vX;   int vY;   int vZ;
  201.     int velX; int velY; int velZ;
  202.     int breakLoop = false;
  203.  
  204.     int magnitude;
  205.  
  206.     int pln = PlayerNumber();
  207.     int playerTid = ActivatorTID();
  208.  
  209.    
  210.  
  211.     if (playerTid == 0) // can we give him a TID without breaking anything?
  212.     {
  213.         playerTid = unusedTID(STARTTID, -1);
  214.         Thing_ChangeTID(0, playerTid);
  215.     }
  216.  
  217.     // if not, then okay, just use the one he has already
  218.  
  219.     int grX = GetActorX(grappleTid);
  220.     int grY = GetActorY(grappleTid);
  221.     int grZ = GetActorZ(grappleTid);
  222.  
  223.     while (!breakLoop)
  224.     {
  225.         if (ThingCount(0, grappleTID) != 1)
  226.         {
  227.             break;
  228.         }
  229.        
  230.         plX  = GetActorX(0);    plY  = GetActorY(0);    plZ  = GetActorZ(0);
  231.         vX   = grX - plX;       vY   = grY - plY;       vZ   = grZ - plZ;
  232.  
  233.         // NOTE: I had to drop precision here in exchange for not overflowing
  234.         magnitude = magnitudeThree(vX >> 16, vY >> 16, vZ >> 16);
  235.  
  236.         if (magnitude != 0)
  237.         {
  238.             nX = vX / magnitude; nY = vY / magnitude; nZ = vZ / magnitude;
  239.             ACS_ExecuteAlways(ROPE_DRAWLINE, 0, grappleTid, playerTid, 16);
  240.         }
  241.         else
  242.         {
  243.             nX = 0; nY = 0; nZ = 0;
  244.         }
  245.  
  246.         SetActorVelocity(0, nX*strength, nY*strength, nZ*strength, true, true);
  247.  
  248.         switch (firetype)
  249.         {
  250.         case 0:
  251.             if (!(GetPlayerInput(-1, MODINPUT_BUTTONS) & BT_ATTACK))
  252.             {
  253.                 breakLoop = true;
  254.             }
  255.             break;
  256.  
  257.         case 1:
  258.             if (!(GetPlayerInput(-1, MODINPUT_BUTTONS) & BT_ALTATTACK))
  259.             {
  260.                 breakLoop = true;
  261.             }
  262.             break;
  263.  
  264.         case 2:
  265.             if (!CheckInventory("GrappleFromInv"))
  266.             {
  267.                 breakLoop = true;
  268.             }
  269.             break;
  270.         }
  271.  
  272.         Delay(1);
  273.     }
  274.  
  275.     Thing_Remove(grappleTid);
  276. }
  277.  
  278.  
  279. script ROPE_PLAYERHOLD (int ropeTID, int length, int strength)
  280. {
  281.     int playerInput = GetPlayerInput(-1, INPUT_BUTTONS);
  282.     int fireType = 0;
  283.  
  284.     if (playerInput & BT_ALTATTACK)
  285.     {
  286.         fireType = BT_ALTATTACK;
  287.     }
  288.     else if (playerInput & BT_ATTACK)
  289.     {
  290.         fireType = BT_ATTACK;
  291.     }
  292.  
  293.     if (ThingCount(0, ropeTID == 1))
  294.     {
  295.         ACS_NamedExecuteAlways("rope_hold", 0, ropeTID, length, strength);
  296.     }
  297.     else
  298.     {
  299.         terminate;
  300.     }
  301.  
  302.     while (GetPlayerInput(-1, INPUT_BUTTONS) & fireType)
  303.     {
  304.         Delay(1);
  305.     }
  306.  
  307.     Thing_Remove(ropeTID);
  308. }
  309.  
  310. script ROPE_SPAWNBARREL (int barTID)
  311. {
  312.     int spawnAngle = GetActorAngle(0);
  313.     int spawnPitch = GetActorPitch(0);
  314.  
  315.     int spawnX     = GetActorX(0);
  316.     int spawnY     = GetActorY(0);
  317.     int spawnZ     = GetActorZ(0);
  318.  
  319.     int newSpawnX  = FixedMul(64 * cos(spawnAngle), cos(spawnPitch));
  320.     int newSpawnY  = FixedMul(64 * sin(spawnAngle), cos(spawnPitch));
  321.     int newSpawnZ  = -sin(spawnPitch);
  322.  
  323.     newSpawnX = spawnX + newSpawnX;
  324.     newSpawnY = spawnY + newSpawnY;
  325.     newSpawnZ = spawnZ + newSpawnZ;
  326.  
  327.     Spawn("PushableBarrel", newSpawnX, newSpawnY, newSpawnZ, barTID, 0);
  328. }
  329.  
  330. script ROPE_DRAWLINE (int grappleTid, int playerTid, int density)
  331. {
  332.     int plX; int plY;  int plZ;
  333.     int grX; int grY;  int grZ;
  334.     int vX;  int vY;   int vZ;
  335.     int nX;  int nY;   int nZ;
  336.     int bX;  int bY;   int bZ;
  337.     int magnitude;
  338.     int pointCount;
  339.     int pointOffset;
  340.  
  341.     if (ThingCount(0, grappleTid) != 1)
  342.     {
  343.         terminate;
  344.     }
  345.  
  346.     if (ThingCount(0, playerTid) != 1)
  347.     {
  348.         terminate;
  349.     }
  350.  
  351.     plX = GetActorX(playerTid); grX = GetActorX(grappleTid);
  352.     plY = GetActorY(playerTid); grY  = GetActorY(grappleTid);
  353.     plZ = GetActorZ(playerTid); grZ  = GetActorZ(grappleTid);
  354.     vX   = grX - plX;       vY   = grY - plY;       vZ   = grZ - plZ;
  355.  
  356.     // NOTE: More precision bullshit
  357.     magnitude = magnitudeThree(vX >> 16, vY >> 16, vZ >> 16);
  358.  
  359.     pointCount  = magnitude / density;
  360.     pointOffset = magnitude - (pointCount * density);
  361.  
  362.     if (magnitude != 0)
  363.     {
  364.         nX = vX / magnitude; nY = vY / magnitude; nZ = vZ / magnitude;
  365.  
  366.         int i; int j;
  367.         for (i = 0; i < pointCount; i++)
  368.         {
  369.             j = (i * density) + pointOffset;
  370.  
  371.             bX = (nX * j) + plX;
  372.             bY = (nY * j) + plY;
  373.             bZ = (nZ * j) + plZ;
  374.  
  375.             Spawn("LinePoint", bX, bY, bZ);
  376.         }
  377.     }
  378.  
  379. }
  380.  
  381. script ROPE_GETUNUSEDTID (int mode, int fromItem)
  382. {
  383.     int newTID;
  384.  
  385.     int itemType = "GrappleTIDIndicator";
  386.     if (fromItem)
  387.     {
  388.         itemType = "ItemGrappleTID";
  389.     }
  390.  
  391.     switch (mode)
  392.     {
  393.     case 0:
  394.         newTID = unusedTID(STARTTID, -1);
  395.  
  396.         GiveInventory(itemType, newTID);
  397.         Thing_ChangeTID(0, newTID);
  398.  
  399.         break;
  400.  
  401.     case 1:
  402.         SetResultValue(CheckInventory(itemType));
  403.         break;
  404.  
  405.     case 2:
  406.         newTID = unusedTID(STARTTID, -1);
  407.  
  408.         GiveInventory(itemType, newTID);
  409.  
  410.         Thing_ChangeTID(0, newTID);
  411.         SetResultValue(newTID);
  412.         break;
  413.     }
  414. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement