Guest User

Farmer .jbot

a guest
Sep 25th, 2013
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //#! name = Farmer
  2. //#! uniq = farmer_apxproductions
  3. //#! icon = gfx/invobjs/seed-hops
  4. // Globals
  5. var inventory = checkInventory();
  6. var harvester;
  7.  
  8. // Settings | Ustawienia
  9. var actionTimeout = 1000 * 60 * 2;   // Lag timeout
  10. var cannotPlantTimeout = 1000 * 10;   // Non-plowed tile timeout
  11. var cropRadius = 3;               // Zasięg kratek, w którym wyszukiwane są kolejne kratki z daną roślinką.
  12.  
  13. // Flags
  14. var stopFlag = false;
  15.  
  16. /* =========================================================================== */
  17. /* ==============================   Marchew     ============================== */
  18. /* =========================================================================== */
  19.  
  20. function CarrotHarvester() {
  21.     this.cropName = "plants/carrot";    //Nazwa obiektu na polu
  22.     this.cropSeedName = "invobjs/carrot";  //Nazwa obiektu w ekwipunku
  23.    this.cropStage = 4;                 //Poziom wzrostu przy, którym zbieramy roślinkę
  24. }
  25. CarrotHarvester.prototype.processHarvestedGoods = function() {
  26.     inventory = checkInventory();
  27.    
  28.    var seeds = inventory.getItems(this.cropSeedName);
  29.    var free = inventory.freeSlots();
  30.    var i = 0;
  31.    if (free < 3) {
  32.       inventory.sortItems(seeds, "quality", false);
  33.       for (i = 0; i < 3 - free; i++) {
  34.          // Powyższą wartość możemy zmienić by zostawić więcej miejsca w eq. np :   for (i = 0; i < 6 - free; i++)
  35.      // Wówczas zostawi się 6 miejsc wolnych.
  36. {
  37.          if (jGetHungry() < 95) {
  38.             if (seeds[i].isActual()) {
  39.                seeds[i].iact();
  40.                jWaitPopup(actionTimeout);
  41.                jSelectContextMenu("Eat");
  42.                waitUnActual(seeds[i]);
  43.             }
  44.          }
  45.          
  46.          if (seeds[i].isActual()) {
  47.             seeds[i].drop();
  48.          }
  49.       }
  50.    }
  51. };
  52.  
  53. /* =========================================================================== */
  54. /* ==============================   Buraki      ============================== */
  55. /* =========================================================================== */
  56. function BeetHarvester() {
  57.     this.cropName = "plants/beetroot";    
  58.     this.cropSeedName = "invobjs/beetroot";  
  59.    this.cropStage = 3;                  
  60. }
  61. BeetHarvester.prototype.processHarvestedGoods = function() {
  62.     inventory = checkInventory();
  63.    
  64.    var seeds = inventory.getItems(this.cropSeedName);
  65.    var free = inventory.freeSlots();
  66.    var i = 0;
  67.    //dropsleaves
  68.    var leaves = inventory.getItems("beetrootleaves");
  69.    if(leaves.length > 0)
  70.       if(leaves[0].isActual())
  71.          leaves[0].dropSuchAll();
  72.    while(leaves.length > 0)
  73.    {
  74.       leaves = inventory.getItems("beetrootleaves");
  75.       jSleep(500);
  76.    }
  77.    if (free < 6) {
  78.       inventory.sortItems(seeds, "quality", false);
  79.       for (i = 0; i < 4 - free; i++) {
  80.          if (jGetHungry() < 95) {
  81.             if (seeds[i].isActual()) {
  82.                seeds[i].iact();
  83.                jWaitPopup(actionTimeout);
  84.                jSelectContextMenu("Eat");
  85.                waitUnActual(seeds[i]);
  86.             }
  87.          }
  88.          if (seeds[i].isActual()) {
  89.             seeds[i].drop();
  90.          }
  91.       }
  92.    }
  93. };
  94.  
  95. /* =========================================================================== */
  96. /* ==============================   Winogrona   ============================== */
  97. /* =========================================================================== */
  98. function WineHarvester() {
  99.     this.cropName = "plants/wine";  
  100.     this.cropSeedName = "invobjs/seed-grape";  
  101.    this.packName = "invobjs/grapes";
  102.    this.cropStage = 3;                  
  103.    this.barrelID = 0; //id beczki
  104.    this.pressID = 0; //id  winepress
  105.    this.containerID = 0;
  106.    this.startCoord = jCoord(0, 0);
  107.    this.fieldSize = jCoord(0, 0);
  108.    this.offsMove = jCoord(2, 0);
  109. }
  110.  
  111. WineHarvester.prototype.selectObjects = function() {
  112.    var bar = jSelectObject("Select barrel...");
  113.    if(bar.name().indexOf("barrel") == -1)
  114.    {
  115.       jToConsole("ERROR: This is not a barrel!");
  116.       return false;
  117.    }
  118.    else this.barrelID = bar;
  119.    var pr = jSelectObject("Select winepress...");
  120.    if(pr.name().indexOf("winepress") == -1)
  121.    {
  122.       jToConsole("ERROR: This is not a press!");
  123.       return false;
  124.    }
  125.    else this.pressID = pr;
  126.    var cnt = jSelectObject("Select container (chest/lchest)...");
  127.    if(cnt.name().indexOf("cclosed") == -1 && cnt.name().indexOf("lchest") == -1)
  128.    {
  129.       jToConsole("ERROR: This is not a container!");
  130.       return false;
  131.    }
  132.    else this.containerID = cnt;
  133.    return true;
  134. }
  135.  
  136. WineHarvester.prototype.selectRect = function() {
  137.    var offs = jCoord(0, 0);
  138.    var size = jCoord(1, 1);
  139.    var confirm = false;
  140.    jDrawGroundRect(offs, size);
  141.    var blist = ["West", "East", "North", "South", "Inc width", "Inc height", "Dec width", "Dec height", "Confirm", "Exit"];
  142.    var selectWindow = jGUIWindow(jCoord(250, 250), jCoord(105, blist.length * 25 + 15), "Test");
  143.    var label = jGUILabel(selectWindow, jCoord(5, 5), "Select grapes area.");
  144.    for(var i = 0; i < blist.length; i++)
  145.       jGUIButton(selectWindow, jCoord(5, 25 + i*25),  100, blist[i]);
  146.    selectWindow.toggleCloseButton();
  147.    var btext = "";
  148.    while(btext != "Exit"){
  149.       btext = selectWindow.waitButtonClick();
  150.       if(btext == blist[0]) offs.x--;
  151.       if(btext == blist[1]) offs.x++;
  152.       if(btext == blist[2]) offs.y--;
  153.       if(btext == blist[3]) offs.y++;
  154.       if(btext == blist[4]) size.x++;
  155.       if(btext == blist[5]) size.y++;
  156.       if(btext == blist[6]) if(size.x > 1) size.x--;
  157.       if(btext == blist[7]) if(size.y > 1) size.y--;
  158.       if(btext == blist[8]) {confirm = true; break;}
  159.       jDrawGroundRect(offs, size);
  160.    }
  161.    selectWindow.destroy();
  162.    jDrawGroundRect(offs, jCoord(0, 0));
  163.    this.startCoord = jMyCoords().add(offs.mul(11));
  164.    this.fieldSize = size;
  165.    return confirm;
  166. }
  167.  
  168. WineHarvester.prototype.processHarvesting = function() {
  169.    var result = false;
  170.    var grapes = jGetObjectsInRect(this.startCoord, this.fieldSize, this.cropStage, [this.cropName]);
  171.    inventory = checkInventory();
  172.    var cuts = Math.floor((inventory.freeSlots() - 1)/3);
  173.    if(cuts > 8) cuts = 8;
  174.    var currentgrape = 0;
  175.    while(1)
  176.    {
  177.       if(currentgrape >= grapes.length) {result = true; break;}
  178.       //cursor
  179.       if(!jIsCursor("harvest")) {
  180.          jSendAction("harvest");
  181.          jWaitCursor("harvest", actionTimeout);
  182.       }
  183.       //harvesting grapes
  184.       for(var i = 0; i < cuts; i++)
  185.       {
  186.          if(jGetStamina() < 50) drinkWater();
  187.          if(currentgrape >= grapes.length) break;
  188.          jDoClick(grapes[currentgrape], 1, 0);
  189.          jWaitProgress(actionTimeout);
  190.          currentgrape++;
  191.       }
  192.       //got pack of grapes
  193.       resetCursor();
  194.       //goes to press
  195.       jAbsClick(this.pressID.position().add(this.offsMove.mul(11)), 1, 0);
  196.       jWaitMove(actionTimeout);
  197.       jDoClick(this.pressID, 3, 0);
  198.       jWaitWindow("Winepress", actionTimeout);
  199.       //no window in lag/etc
  200.       jSleep(100);
  201.       if(!jHaveWindow("Winepress")) {result = false; break;}
  202.       //transfers all grapes to winepress
  203.       inventory.getItems(this.packName)[0].transferSuchAll();
  204.       //waits transfering
  205.       while(inventory.getItems(this.packName).length > 0)
  206.          jSleep(300);
  207.       //making juice
  208.       jGetWindow("Winepress").pushButton(1);
  209.       while(jGetWindow("Winepress").getInventories()[0].getItems(this.packName).length > 0)
  210.          jSleep(500);
  211.       //lifts press
  212.       var pc = this.pressID.position();// memorizing press coords
  213.       jSendAction("carry");
  214.       jWaitCursor("chi", actionTimeout);
  215.       //lag
  216.       if(!jIsCursor("chi")) {result = false; break;}
  217.       jDoClick(this.pressID, 1, 0);
  218.       jSleep(1500);
  219.       /*while(!jAvatarIsCarrying(jGetMyID()))
  220.          jSleep(200);*/
  221.       //juice to barrel
  222.       jDoClick(this.barrelID, 3, 0);
  223.       jWaitMove(actionTimeout);
  224.       jSleep(2000);
  225.       //placing press back
  226.       jAbsClick(pc, 3, 0);
  227.       jWaitMove(actionTimeout);
  228.       /*while(jAvatarIsCarrying(jGetMyID()))
  229.          jSleep(200);*/
  230.       //getting seeds from press
  231.       jDoClick(this.pressID, 3, 0);
  232.       jWaitWindow("Winepress", actionTimeout);
  233.       //no window in lag/etc
  234.       jSleep(100);
  235.       if(!jHaveWindow("Winepress")) {result = false; break;}
  236.       //transfer to pack seeds
  237.       jGetWindow("Winepress").getInventories()[0].getItems("")[0].transferSuchAll();
  238.       while(jGetWindow("Winepress").getInventories()[0].getItems("").length > 0)
  239.          jSleep(200);
  240.       //offset move
  241.       jMoveStep(this.offsMove);
  242.       jWaitMove(actionTimeout);
  243.       //going to lchect
  244.       var lc = this.containerID.position().add(this.offsMove.mul(11));
  245.       jAbsClick(lc, 1, 0);
  246.       jWaitMove(actionTimeout);
  247.       jDoClick(this.containerID, 3, 0);
  248.       jWaitWindow("Chest", actionTimeout);
  249.       //no window in lag/etc
  250.       jSleep(100);
  251.       if(!jHaveWindow("Chest")) {result = false; break;}
  252.       var bags = jGetWindow("Chest").getInventories()[0].getItems("invobjs/bag-seed");
  253.       var cbag = 0; //current bag
  254.       inventory = checkInventory();
  255.       //transferring seeds, btw i hate loftar
  256.       while(inventory.getItems(this.cropSeedName).length >= 1)
  257.       {
  258.          if(cbag >= bags.length) {result = true; break;}
  259.          var cnt = inventory.getItems(this.cropSeedName).length; //inventory count
  260.          bags[cbag].iact();
  261.          //opens seedbag
  262.          while(!jHaveWindow("Seedbag"))
  263.             jSleep(200);
  264.          //check for free slots
  265.          if(jGetWindow("Seedbag").getInventories()[0].freeSlots() == 0)
  266.          {
  267.             jGetWindow("Seedbag").close();
  268.             while(jHaveWindow("Seedbag"))
  269.                jSleep(200);
  270.             cbag++;
  271.             continue;
  272.          }
  273.          jSleep(100);
  274.          var free = jGetWindow("Seedbag").getInventories()[0].freeSlots(); //free slots in seedbag
  275.          inventory.getItems(this.cropSeedName)[0].transferSuchAll();
  276.          while(inventory.getItems(this.cropSeedName).length > Math.abs(cnt - free))
  277.             jSleep(200);
  278.          jGetWindow("Seedbag").close();
  279.          while(jHaveWindow("Seedbag"))
  280.             jSleep(200);
  281.          cbag++;
  282.       }
  283.    
  284.       if(result == true) {inventory.getItems(this.cropSeedName)[0].dropSuchAll(); jSleep(1000); break;} //break if all bags r full
  285.       //checks barrel
  286.       var bc = this.barrelID.position().add(this.offsMove.mul(11));
  287.       jAbsClick(bc, 1, 0);
  288.       jWaitMove(actionTimeout);
  289.       jDoClick(this.barrelID, 3, 0);
  290.       while(!jHaveWindow("Barrel"))
  291.          jSleep(500);
  292.       //barrel label
  293.       var blbl = jGetWindow("Barrel").getLabelText(1);
  294.       if(blbl.indexOf("100.0") != -1) {result = true; break;} //breaks if barrel is full
  295.    }//1
  296.    return result;
  297. }
  298.  
  299. WineHarvester.prototype.plantGrapes = function() {
  300.    jMoveStep(this.offsMove);
  301.    jWaitMove(actionTimeout);
  302.    //going to lchect
  303.    var lc = this.containerID.position().add(this.offsMove.mul(11));
  304.    jAbsClick(lc, 1, 0);
  305.    jWaitMove(actionTimeout);
  306.    jDoClick(this.containerID, 3, 0);
  307.    jWaitWindow("Chest", actionTimeout);
  308.    //no window in lag/etc
  309.    jSleep(100);
  310.    if(!jHaveWindow("Chest")) return false;
  311.    jGetWindow("Chest").getInventories()[0].getItems("invobjs/bag-seed-f")[0].transferSuchAll();
  312.    jSleep(1000);
  313.    inventory = checkInventory();
  314.    var fullbags = inventory.getItems("invobjs/bag-seed-f");
  315.    for(var i = 0; i < fullbags.length; i++)
  316.       fullbags[i].iact();
  317.    while(jGetWindows("Seedbag").length < fullbags.length)
  318.       jSleep(200);
  319.    var swnds = jGetWindows("Seedbag");
  320.    var allseeds = [];
  321.    for(var i = 0; i < swnds.length; i++)
  322.    {
  323.       var sinvs = swnds[i].getInventories()[0].getItems(this.cropSeedName);
  324.       for(var j = 0; j < sinvs.length; j++)
  325.          allseeds.push(sinvs[j]);
  326.    }
  327.    //allseeds.sort(sortFunction2);
  328.    var cseed = 0;
  329.    var overflow = false;
  330.    jAbsClick(jTilify(this.startCoord), 1, 0);
  331.    jWaitMove(actionTimeout);
  332.    for(var i = 0; i < this.fieldSize.x; i++) {
  333.       if(overflow == true) break;
  334.       for(var j = 0; j < this.fieldSize.y; j++) {
  335.          if(cseed >= allseeds.length) {overflow = true; break;}
  336.          if(jGetStamina() < 50) drinkWater();
  337.          jAbsClick(this.startCoord.add(i*11, j*11));
  338.          jWaitMove(actionTimeout);
  339.          if(jFindObjectByName(this.cropName, 3) != 0) continue;
  340.          plantNewCrop(this, false);
  341.          /*if(!jGetTileType(jCoord(0, 0)) != 9) plowTile();
  342.          allseeds[cseed].take();
  343.          jWaitDrag(actionTimeout);
  344.          jAbsInteractClick(jCoord(0, 0), 3, 0);
  345.          jWaitDrop(actionTimeout);
  346.          jSleep(100);*/
  347.          cseed++;
  348.       }
  349.       jSleep(100);
  350.    }
  351.    jToConsole("test");
  352. }
  353. /* =========================================================================== */
  354. /* ==============================   Konopia     ============================== */
  355. /* =========================================================================== */
  356. function HempHarvester() {
  357.     this.cropName = "plants/hemp";    
  358.     this.cropSeedName = "invobjs/seed-hemp";
  359.    this.cropStage = 4;              //Warto zmienić gdy szukamy topów do palenia na cropStage = 3;
  360.    bagsSeedsSorter(this);
  361. }
  362. HempHarvester.prototype.processHarvestedGoods = function() {
  363.     bagsSeedsSorter(this);
  364. };
  365.  
  366. /* =========================================================================== */
  367. /* ==============================   Len         ============================== */
  368. /* =========================================================================== */
  369. function FlaxHarvester() {
  370.     this.cropName = "plants/flax";    
  371.     this.cropSeedName = "invobjs/flaxseed";  
  372.    this.cropStage = 3;              
  373. }
  374. FlaxHarvester.prototype.processHarvestedGoods = function() {
  375.     bagsSeedsSorter(this);
  376. };
  377.  
  378. /* =========================================================================== */
  379. /* ==============================   Pieprz      ============================== */
  380. /* =========================================================================== */
  381. function PepperHarvester() {
  382.     this.cropName = "plants/pepper";
  383.     this.cropSeedName = "invobjs/seed-pepper";  
  384.    this.cropStage = 3;            
  385. }
  386. PepperHarvester.prototype.processHarvestedGoods = function() {
  387.     bagsSeedsSorter(this);
  388. };
  389.  
  390. /* =========================================================================== */
  391. /* ==============================  Herbata      ============================== */
  392. /* =========================================================================== */
  393. function TeaHarvester() {
  394.     this.cropName = "plants/tea";    
  395.     this.cropSeedName = "invobjs/seed-tea";
  396.    this.cropStage = 3;                
  397.    this.leaveName = "invobjs/tea-fresh";
  398.    this.containerID = 0;
  399. }
  400. TeaHarvester.prototype.processHarvestedGoods = function() {
  401.     bagsSeedsSorter(this);
  402. };
  403.  
  404. TeaHarvester.prototype.selectObjects = function() {
  405.    var cnt = jSelectObject("Select container (chest/lchest)...");
  406.    if(cnt.name().indexOf("cclosed") == -1 && cnt.name().indexOf("lchest") == -1)
  407.    {
  408.       jToConsole("ERROR: This is not a container!");
  409.       return false;
  410.    }
  411.    else this.containerID = cnt;
  412.    return true;
  413. }
  414. /* =========================================================================== */
  415. /* ==============================     Mak       ============================== */
  416. /* =========================================================================== */
  417. function PoppyHarvester() {
  418.     this.cropName = "plants/poppy";      
  419.     this.cropSeedName = "invobjs/seed-poppy";  
  420.    this.cropStage = 4;              
  421. }
  422. PoppyHarvester.prototype.processHarvestedGoods = function() {
  423.    bagsSeedsSorter(this);
  424. };
  425.  
  426. /* =========================================================================== */
  427. /* ==============================  Pszenica     ============================== */
  428. /* =========================================================================== */
  429. function WheatHarvester() {
  430.     this.cropName = "plants/wheat";  
  431.     this.cropSeedName = "wheat";
  432.    this.cropStage = 3;                
  433. }
  434. WheatHarvester.prototype.processHarvestedGoods = function() {
  435.     bagsSeedsSorter(this);
  436. };
  437.  
  438.  
  439. function bagsSeedsSorter(harv) {
  440.    inventory = checkInventory();
  441.    var bags = inventory.getItems("invobjs/bag-seed");
  442.    var free = inventory.freeSlots();
  443.    var fslots = 0;
  444.    
  445.    if (bags.length > 0) {
  446.       var windows = jGetWindows("Seedbag");
  447.       if (windows.length > 0) {
  448.          var seeds = [];
  449.          var maxseedscount = bags.length * 9;
  450.          //getting seeds from all seedbags into array
  451.          for (var i = 0; i < windows.length; i++) {
  452.             var inv = waitInventory(windows[i], 0);
  453.             fslots += inv.freeSlots();
  454.             var bufseeds = inv.getItems(harv.cropSeedName);
  455.             for (var j = 0; j < bufseeds.length; j++) {
  456.                seeds.push(bufseeds[j]);
  457.             }
  458.          }
  459.          //from inventory to same array
  460.          var invseeds = inventory.getItems(harv.cropSeedName);
  461.          for (var j = 0; j < invseeds.length; j++) {
  462.             seeds.push(invseeds[j]);
  463.          }
  464.          //calculation quality of all seeds
  465.          var allq = 0;
  466.          for (var j = 0; j < seeds.length; j++) {
  467.             allq += seeds[j].quality();
  468.          }
  469.          //medium quality
  470.          var mediumrange = (allq / seeds.length) - 1;
  471.          //correcting medium q
  472.          //too many seeds
  473.          if(fslots < 9)
  474.             mediumrange += 1;
  475.          
  476.          if (seeds.length > maxseedscount * 0.75) {
  477.             for (var j = 0; j < seeds.length; j++) {
  478.                if (seeds[j].quality() <= mediumrange) seeds[j].drop();
  479.             }
  480.             organizeBags(inventory, harv.cropSeedName);
  481.          }
  482.       }//bags windows
  483.    }
  484.    if (inventory.freeSlots() < 4 && bags.length < 1) {
  485.       inventory.sortItems(inventory.getItems(harv.cropSeedName), "quality", false);
  486.       for (i = 0; i < 3 - free; i++) {
  487.          if (seeds[i].isActual()) {
  488.             seeds[i].drop();
  489.          }
  490.       }
  491.    }
  492. }
  493.  
  494. function organizeBags(inv, name) {
  495.    var bagw = jGetWindows("Seedbag");
  496.    if(bagw.length < 1) return;
  497.    var fullb, nextb;
  498.    fullb = 0;
  499.    nextb = bagw.length - 1;
  500.    while(1)
  501.    {
  502.       if(fullb == nextb) break; //breaks if filling bag == last bag
  503.       if(bagw[fullb].getInventories()[0].freeSlots() == 0) {
  504.          fullb++;
  505.          continue;
  506.       }
  507.       if(bagw[nextb].getInventories()[0].freeSlots() == 9) {
  508.          nextb--;
  509.          continue;
  510.       }
  511.       var freeSlot = bagw[fullb].getInventories()[0].freeSlotsCoords()[0]; //getting 1st empty slot at current filling bag
  512.       var seed = bagw[nextb].getInventories()[0].getItems(name)[0];    //getting 1st seed at current emptying bag
  513.       seed.take();
  514.       jWaitDrag();
  515.       bagw[fullb].getInventories()[0].drop(freeSlot); //drops to filling bag
  516.       jWaitDrop();
  517.    }
  518. }
  519.  
  520. function openAllSeedbags() {
  521.    inv = checkInventory();
  522.    var bags = inv.getItems("invobjs/bag-seed");
  523.    if(bags.length < 1)
  524.       return;
  525.    for(var i = 0; i < bags.length; i++)
  526.       if(bags[i].isActual())
  527.          bags[i].iact();
  528.    while(jGetWindows("Seedbag").length != bags.length)
  529.       jSleep(300);
  530. }
  531.  
  532. function waitUnActual(item) {
  533.    while (item.isActual()) {
  534.       jSleep(100);
  535.    }
  536. }
  537.  
  538. function resetCursor() {
  539.    if (!jIsCursor("arw")) {
  540.       jAbsClick(jCoord(0, 0), 3, 0);
  541.       jWaitCursor("arw", actionTimeout);
  542.    }
  543. }
  544.  
  545. function checkInventory() {
  546.    if(!jHaveWindow("Inventory")) {
  547.       jToggleInventory();
  548.       while(!jHaveWindow("Inventory"))
  549.          jSleep(100);
  550.    }
  551.    return jGetWindow("Inventory").getInventories()[0];
  552. }
  553.  
  554. function checkEquipment() {
  555.    if(!jHaveWindow("Equipment")) {
  556.       jToggleEquipment();
  557.       while(!jHaveWindow("Equipment"))
  558.          jSleep(100);
  559.    }
  560.    return jGetJSEquip();
  561. }
  562.  
  563. function waitInventoryObject(inv, objname) {
  564.    while (true) {
  565.       var objs = inv.getItems(objname);
  566.       if (objs.length > 0) break;
  567.       else jSleep(100);
  568.    }  
  569. }
  570.  
  571. function waitUnActual(item) {
  572.    while (item.isActual()) {
  573.       jSleep(100);
  574.    }
  575. }
  576.  
  577. function drinkWater() {
  578.    if (jGetStamina() > 80) return;
  579.    inventory = checkInventory();
  580.    var buckets = inventory.getItems("bucket-water");
  581.    if (buckets.length > 0) {
  582.       inventory.sortItems(buckets, "amount", false);
  583.       var bucket = buckets[0];
  584.       var bucket_coord = bucket.coord();
  585.       if (bucket.isActual()) {
  586.          bucket.take();
  587.          jWaitDrag();
  588.          var flasks = inventory.getItems("waterflask", "waterskin");
  589.          if (flasks.length > 0) {
  590.             var flask = flasks[0];
  591.             if (flask.isActual()) {
  592.                flask.itemact(0);
  593.                jSleep(500);
  594.                inventory.drop(bucket_coord);
  595.                jWaitDrop();
  596.             }
  597.          }
  598.       }
  599.    }
  600.    var flasks = inventory.getItems("waterflask", "waterskin");
  601.    if (flasks.length > 0) {
  602.       var flask = flasks[0];
  603.       if (flask.isActual()) {
  604.          flask.iact();
  605.          if (jWaitPopup(actionTimeout)) {
  606.             jSelectContextMenu("Drink");
  607.             jWaitProgress();
  608.          } else {
  609.             // No water
  610.             stopFlag = true;
  611.          }
  612.       }
  613.    }
  614. }
  615.  
  616. function TileInfo(crd) {
  617.    this.coord = crd;
  618. }
  619.  
  620. function findCrop(harv) {
  621.    var objid = 0;
  622.    for (var i = 0; i < cropRadius; i++) {
  623.       objid = (jFindObjectWithOffset(harv.cropName, 1, jCoord(0, i))); // down
  624.       if (objid && objid.blob(0) == harv.cropStage) return objid;
  625.    }
  626.    for (var i = 0; i < cropRadius; i++) {
  627.       objid = jFindObjectWithOffset(harv.cropName, 1, jCoord(0, -i)); // up
  628.       if (objid && objid.blob(0) == harv.cropStage) return objid;
  629.    }
  630.    for (var i = 0; i < cropRadius; i++) {
  631.       objid = jFindObjectWithOffset(harv.cropName, 1, jCoord(i, 0)); // right
  632.       if (objid && objid.blob(0) == harv.cropStage) return objid;
  633.    }
  634.    for (var i = 0; i < cropRadius; i++) {
  635.       objid = jFindObjectWithOffset(harv.cropName, 1, jCoord(-i, 0));// left
  636.       if (objid && objid.blob(0) == harv.cropStage) return objid;
  637.    }
  638.    objid = jFindObjectByName(harv.cropName, cropRadius);
  639.    if (objid.blob(0) != harv.cropStage) objid = -1;
  640.    return objid;
  641. }
  642.  
  643. function dropItem(coord) {
  644.    var items = inventory.getItems("");
  645.    for (var i = 0; i < items.length; i++) {
  646.       if (items[i].coord().x == coord.x && items[i].coord().y == coord.y) {
  647.          items[i].drop();
  648.          break;
  649.       }
  650.    }  
  651. }
  652.  
  653. function waitDragName(name) {
  654.    while (true) {
  655.       var item = jGetDraggingItem();
  656.       if (item != null) {
  657.          if (item.resName() != null) {
  658.          if (item.resName().indexOf(name) >= 0) {
  659.             break;
  660.          } else {
  661.             jSleep(100);
  662.          }
  663.          } else jSleep(100);
  664.       } else {
  665.          break;
  666.       }
  667.    }
  668. }
  669.  
  670. function equipScythe() {
  671.    var equip = checkEquipment();
  672.    var scythe = inventory.getItems("scythe")[0];
  673.    if (!scythe) return;
  674.    if (equip.resName(6).indexOf("shovel") >= 0) {
  675.       equip.takeAt(6);
  676.       jWaitDrag(actionTimeout);
  677.       inventory.drop(scythe.coord());
  678.       jSleep(1000);
  679.       waitDragName("scythe");
  680.       equip.dropTo(6);
  681.       jWaitDrop(actionTimeout);
  682.    }
  683. }
  684.  
  685. function equipShovel() { // From scythe
  686.    var equip = checkEquipment();
  687.    var shovel = inventory.getItems("shovel")[0];
  688.    if (!shovel) return;
  689.    if (equip.resName(6).indexOf("scythe") >= 0) {
  690.       dropItem(shovel.coord().add(0, 2));
  691.       equip.takeAt(6);
  692.       jWaitDrag(actionTimeout);
  693.       inventory.drop(shovel.coord());
  694.       jSleep(1000);
  695.       waitDragName("shovel");
  696.       equip.dropTo(6);
  697.       jWaitDrop(actionTimeout);
  698.    }
  699. }
  700.  
  701. function plowTile() {
  702.    drinkWater();
  703.    equipShovel();
  704.    jSendAction("plow");
  705.    jWaitCursor("dig", actionTimeout);
  706.    jOffsetClick(jCoord(0, 0), 1, 0);
  707.    jWaitProgress();
  708.    jOffsetClick(jCoord(0, 0), 3, 0);
  709.    jWaitCursor("arw", actionTimeout);
  710. }
  711.  
  712. function sayArea(text) {
  713.    var chats = jGetChats();
  714.    for (var i = 0; i < chats.length; i++) {
  715.       if (chats[i].chatName().indexOf("Area Chat") >= 0) {
  716.          chats[i].sendMessage(text);
  717.          break;
  718.       }
  719.    }
  720. }
  721.  
  722. function sortFunction2(a, b) {
  723.    if (!a.isActual() || !b.isActual()) return 0;
  724.    else return a.quality() - b.quality();
  725. }
  726.  
  727. function sortFunction(a, b) {
  728.    if (!a.isActual() || !b.isActual()) return 0;
  729.    if (a.quality() < b.quality()) return 1;
  730.    if (a.quality() > b.quality()) return -1;
  731.    return 0;
  732. }
  733.  
  734. function waitInventory(wnd, index) {
  735.    while (wnd.getInventories().length <= 0) {
  736.       jSleep(100);
  737.    }
  738.    return wnd.getInventories()[index];
  739. }
  740.  
  741. function plantNewCrop(harv, notrecursive) {
  742.    jSleep(300);
  743.    inventory = checkInventory();
  744.    var plantedSeed = null;
  745.    var windows = jGetWindows("Seedbag");
  746.    var seedbags = inventory.getItems("invobjs/bag-seed-f");
  747.    if (seedbags.length > 0) {
  748.       var seeds = [];
  749.       for (var i = 0; i < windows.length; i++) {
  750.          var inv = waitInventory(windows[i], 0);
  751.          if (inv) {
  752.             var bufseeds = inv.getItems(harv.cropSeedName);
  753.             for (var j = 0; j < bufseeds.length; j++) {
  754.                seeds.push(bufseeds[j]);
  755.             }
  756.          }
  757.       }
  758.       var invseeds = inventory.getItems(harv.cropSeedName);
  759.       for (var j = 0; j < invseeds.length; j++) {
  760.          seeds.push(invseeds[j]);
  761.       }
  762.       var sorted_seeds = seeds.sort(sortFunction);
  763.       plantedSeed = sorted_seeds[0];
  764.    } else {
  765.       var invseeds = inventory.getItems(harv.cropSeedName);
  766.       if (invseeds.length > 0) {
  767.          inventory.sortItems(invseeds, "quality", true);
  768.          plantedSeed = invseeds[0];
  769.       }
  770.    }
  771.    
  772.    if (plantedSeed) {
  773.       // Plow tile if needed
  774.       if (jGetTileType(jCoord(0, 0)) != 9) {
  775.          plowTile();
  776.       }
  777.       if (plantedSeed.isActual()) {
  778.          plantedSeed.take();
  779.          jWaitDrag();
  780.          jInteractClick(jCoord(0, 0), 0);
  781.          if (!jWaitDrop(cannotPlantTimeout)) {
  782.             jWaitDrop();
  783.             if (notrecursive) return;
  784.             plowTile();
  785.             plantNewCrop(harv, true);
  786.          }
  787.       } else {
  788.          if (!notrecursive) plantNewCrop(harv, true);
  789.          else jPrint("Double fail on non-actual seed");
  790.       }
  791.    }
  792. }
  793.  
  794. function waitPFEndMove() {
  795.    while (true) {
  796.       jWaitEndMove();
  797.       jSleep(500);
  798.       if (!jIsMoving()) {
  799.          return;
  800.       }
  801.    }
  802. }
  803.  
  804. function main() {
  805.    jDropLastWindow();
  806.    openAllSeedbags();
  807.    var blist = ["Carrot", "Poppy", "Wheat", "Hemp", "Beetroot", "Grapes", "Tea", "Flax", "Pepper", "Exit"];
  808.    var selectWindow = jGUIWindow(jCoord(250, 250), jCoord(110, blist.length * 25 + 15), "Farming");
  809.    var label = jGUILabel(selectWindow, jCoord(5, 5), "Select crop type:");
  810.    for(var i = 0; i < blist.length; i++)
  811.       jGUIButton(selectWindow, jCoord(5, 25 + i*25),  100, blist[i]);
  812.    selectWindow.toggleCloseButton();
  813.    var btext = selectWindow.waitButtonClick();
  814.    if(btext == blist[0]) harvester = new CarrotHarvester();
  815.    if(btext == blist[1]) harvester = new PoppyHarvester();
  816.    if(btext == blist[2]) harvester = new WheatHarvester();
  817.    if(btext == blist[3]) harvester = new HempHarvester();
  818.    if(btext == blist[4]) harvester = new BeetHarvester();
  819.    if(btext == blist[5]) {
  820.       selectWindow.destroy();
  821.       harvester = new WineHarvester();
  822.       if(!harvester.selectObjects()) return;
  823.       if(!harvester.selectRect()) return;
  824.       if(!harvester.processHarvesting()) return;
  825.       harvester.plantGrapes();
  826.       return;
  827.    }
  828.    if(btext == blist[6]) {
  829.       harvester = new TeaHarvester();
  830.       if(!harvester.selectObjects()) return;
  831.    }
  832.    if(btext == blist[7]) harvester = new FlaxHarvester();
  833.    if(btext == blist[8]) harvester = new PepperHarvester();
  834.    if(btext == blist[blist.length - 1]) {selectWindow.destroy(); return;}
  835.    selectWindow.destroy();
  836.    while (!stopFlag) {
  837.       var crop = findCrop(harvester);
  838.       if (crop == -1) break; // No crops found
  839.       drinkWater();
  840.       equipScythe();
  841.       if (!jIsPathFree(crop.position())) {
  842.          resetCursor();
  843.          jPFMove(crop.position());
  844.          jWaitStartMove();
  845.          waitPFEndMove();
  846.          while (jIsMoving() || jMyCoords().dist(crop.position()) > 1) {
  847.             jSleep(100);
  848.          }
  849.       }
  850.       jSendAction("harvest");
  851.       jWaitCursor("harvest", actionTimeout);
  852.       jDoClick(crop.getID(), 1, 0);
  853.       jWaitProgress();
  854.       harvester.processHarvestedGoods();
  855.       plantNewCrop(harvester, false);
  856.    }
  857. }
  858.  
  859. main();
Advertisement
Add Comment
Please, Sign In to add comment