Guest User

Liquid Pump System

a guest
Oct 9th, 2011
173
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* --- Pump --- */
  2.  
  3. local pInPipe;
  4. local pOutPipe;
  5. local aMat; //0=Mat;1=Amount
  6.  
  7. public func Timer()
  8. {
  9.     //Energycheck
  10.     //Pump is empty
  11.     if ((aMat[1] == 0) || (aMat[0] == -1))
  12.     {
  13.         //Get new Mats
  14.         aMat = pInPipe->GetLiquid(-1, 5, this);
  15.         //No Material to pump
  16.         if (aMat[1] == 0)
  17.             return 0;
  18.     }
  19.     aMat[1] -= BoundBy(pOutPipe->PutLiquid(aMat[0], aMat[1], this), 0, aMat[1]);
  20.     return 1;
  21. }
  22.  
  23. /* --- LiquidPipe --- */
  24.  
  25. //returns extracted mat + amount
  26. public func GetLiquid(int dnMat, int dnMaxAmount, object pPump)
  27. {
  28.     var aMat;
  29.     if (GetActionTarget(0) != pPump)
  30.         aMat = GetActionTarget(0)->~LiquidOutput(dnMat, dnMaxAmount, pPump, this);
  31.     else
  32.         if (GetActionTarget(1) != pPump)
  33.             aMat = GetActionTarget(1)->~LiquidOutput(dnMat, dnMaxAmount, pPump, this);
  34.     if (GetType(aMat) != C4V_Array)
  35.         return [-1, 0];
  36.     if ((aMat[0] == -1) || (GetLength(aMat) == 1))
  37.         aMat[1] = 0;
  38.     if (aMat[1] < 0)
  39.         aMat[0] = -1;
  40.     return aMat;
  41. }
  42.  
  43. //returns inserted mat amount
  44. public func PutLiquid(int dnMat, int dnMaxAmount, object pPump)
  45. {
  46.     if (dnMat == -1)
  47.         return 0;
  48.     if (GetActionTarget(0) != pPump)
  49.         return BoundBy(GetActionTarget(0)->~LiquidInput(dnMat, dnMaxAmount, pPump, this), 0, dnMaxAmount);
  50.     else
  51.         if (GetActionTarget(1) != pPump)
  52.             return BoundBy(GetActionTarget(1)->~LiquidInput(dnMat, dnMaxAmount, pPump, this), 0, dnMaxAmount);
  53.     return 0;
  54. }
  55.  
  56. /* --- Sample Building (Tank/Reservoir) --- */
  57.  
  58. local dMat;
  59. local dMatAmount;
  60. local dMatMaxAmount;
  61.  
  62. //returns extracted mat + amount
  63. public func LiquidOutput(int dnMat, int dnMaxAmount, object pPump, object pPipe)
  64. {
  65.     //Wrong material?
  66.     if (dnMat == -1)
  67.         dnMat = dMat;
  68.     if ((dnMat != dMat) || (dMat == -1))
  69.         dnMaxAmount = 0;
  70.     dnMaxAmount = Min(dnMaxAmount, dMatAmount);
  71.     dMatAmount -= dnMaxAmount;
  72.     return [dMat, dnMaxAmount];
  73. }
  74.  
  75. //returns inserted mat amount
  76. public func LiquidInput(int dnMat, int dnMaxAmount, object pPump, object pPipe)
  77. {
  78.     if (dnMat != dMat)
  79.         return 0;
  80.     dnMaxAmount = Min(dMatMaxAmount - dMatAmount, dnMaxAmount);
  81.     dMatAmount += dnMaxAmount;
  82.     return dnMaxAmount;
  83. }
  84.  
  85. /* --- Sample Building (Base/Barrelstorage)--- */
  86.  
  87. //returns extracted mat + amount
  88. public func LiquidOutput(int dnMat, int dnMaxAmount, object pPump, object pPipe)
  89. {
  90.     if (dnMat == -1)
  91.     {
  92.         var ptBarrel = FindObject(Find_Container(this), Find_Func("IsBarrel"), Find_Not(Find_Func("IsEmpty")));
  93.         if (ptBarrel)
  94.             dnMat = ptBarrel->GetBarrelMaterial(); //i don't know the real name...
  95.         //Nothing to pump
  96.         if (dnMat == -1)
  97.             return [-1, 0];
  98.     }
  99.     var dtFound = 0;
  100.     for (var ptBarrel in FindObjects(Find_Container(this), Find_Func("IsBarrel"), Find_Func("IsContainerForMaterial", dnMat)))
  101.     {
  102.         dtFound += BoundBy(ptBarrel->GetLiquid(dnMat, dnMaxAmount - dtFound, this), 0, dnMaxAmount);
  103.         if (dtFound == dnMaxAmount)
  104.             break;
  105.     }
  106.     return [dnMat, dtFound];
  107. }
  108.  
  109. //returns inserted mat amount
  110. public func LiquidInput(int dnMat, int dnMaxAmount, object pPump, object pPipe)
  111. {
  112.     var dtAmount = dnMaxAmount;
  113.     for (var ptBarrel in FindObjects(Find_Container(this), Find_Func("IsBarrel"), Find_Func("IsContainerForMaterial", dnMat), Find_Not(Find_Func("IsFull"))))
  114.     {
  115.         dtAmount -= BoundBy(ptBarrel->PutLiquid(dnMat, dtAmount, this), 0, dtAmount);
  116.         if (!dtAmount)
  117.             return dnMaxAmount;
  118.     }
  119.     for (var ptBarrel in FindObjects(Find_Container(this), Find_Func("IsBarrel"), Find_ID(EmptyBarrel)))
  120.     {
  121.         dtAmount -= BoundBy(ptBarrel->PutLiquid(dnMat, dtAmount, this), 0, dtAmount);
  122.         if (!dtAmount)
  123.             return dnMaxAmount;
  124.     }
  125.     return dnMaxAmount - dtAmount;
  126. }
  127.  
RAW Paste Data