//#! name = AFarmer //#! uniq = farmer_apxproductions //#! icon = gfx/invobjs/seed-hops // Globals var inventory = checkInventory(); var harvester; // Settings var actionTimeout = 1000 * 60 * 2; var cannotPlantTimeout = 1000 * 10; var cropRadius = 5; // Flags var stopFlag = false; function CarrotHarvester() { this.cropName = "plants/carrot"; // Имя кропа который нужно харвестить this.cropSeedName = "invobjs/carrot"; // Имя семечки которую нужно сажать this.cropStage = 4; // Стадия обработки } CarrotHarvester.prototype.processHarvestedGoods = function() { inventory = checkInventory(); var seeds = inventory.getItems(this.cropSeedName); var free = inventory.freeSlots(); var i = 0; if (free < 3) { inventory.sortItems(seeds, "quality", false); for (i = 0; i < 3 - free; i++) { // Есть морковь если голоден if (jGetHungry() < 95) { if (seeds[i].isActual()) { seeds[i].iact(); jWaitPopup(actionTimeout); jSelectContextMenu("Eat"); waitUnActual(seeds[i]); } } // Выбросить лишку if (seeds[i].isActual()) { seeds[i].drop(); } } } }; /* =========================================================================== */ /* ============================== СВЕКЛА ============================== */ /* =========================================================================== */ function BeetHarvester() { this.cropName = "plants/beetroot"; // Имя кропа который нужно харвестить this.cropSeedName = "invobjs/beetroot"; // Имя семечки которую нужно сажать this.cropStage = 3; // Стадия обработки } BeetHarvester.prototype.processHarvestedGoods = function() { inventory = checkInventory(); var seeds = inventory.getItems(this.cropSeedName); var free = inventory.freeSlots(); var i = 0; //dropsleaves var leaves = inventory.getItems("beetrootleaves"); if(leaves.length > 0) if(leaves[0].isActual()) leaves[0].dropSuchAll(); while(leaves.length > 0) { leaves = inventory.getItems("beetrootleaves"); jSleep(500); } if (free < 6) { inventory.sortItems(seeds, "quality", false); for (i = 0; i < 4 - free; i++) { if (jGetHungry() < 95) { if (seeds[i].isActual()) { seeds[i].iact(); jWaitPopup(actionTimeout); jSelectContextMenu("Eat"); waitUnActual(seeds[i]); } } if (seeds[i].isActual()) { seeds[i].drop(); } } } }; /* =========================================================================== */ /* ============================== ВИНОГРАД ============================== */ /* =========================================================================== */ function WineHarvester() { this.cropName = "plants/wine"; this.cropSeedName = "invobjs/seed-grape"; this.cropStage = 3; } WineHarvester.prototype.processHarvestedGoods = function() { inventory = checkInventory(); var free = inventory.freeSlots(); var i = 0; //dropsleaves var leaves = inventory.getItems("grapes"); if(leaves.length > 0) if(leaves[0].isActual()) leaves[0].dropSuchAll(); while(leaves.length > 0) { leaves = inventory.getItems("grapes"); jSleep(500); } }; /* =========================================================================== */ /* ============================== КОНОПЛЯ ============================== */ /* =========================================================================== */ function HempHarvester() { this.cropName = "plants/hemp"; // Имя кропа который нужно харвестить this.cropSeedName = "invobjs/seed-hemp"; // Имя семечки которую нужно сажать this.cropStage = 4; // Стадия обработки bagsSeedsSorter(this); } HempHarvester.prototype.processHarvestedGoods = function() { bagsSeedsSorter(this); }; /* =========================================================================== */ /* ============================== ЛЕН ============================== */ /* =========================================================================== */ function FlaxHarvester() { this.cropName = "plants/flax"; // Имя кропа который нужно харвестить this.cropSeedName = "invobjs/flaxseed"; // Имя семечки которую нужно сажать this.cropStage = 3; // Стадия обработки } FlaxHarvester.prototype.processHarvestedGoods = function() { bagsSeedsSorter(this); }; /* =========================================================================== */ /* ============================== ПЕРЕЦ ============================== */ /* =========================================================================== */ function PepperHarvester() { this.cropName = "plants/pepper"; // Имя кропа который нужно харвестить this.cropSeedName = "invobjs/seed-pepper"; // Имя семечки которую нужно сажать this.cropStage = 3; // Стадия обработки } PepperHarvester.prototype.processHarvestedGoods = function() { bagsSeedsSorter(this); }; /* =========================================================================== */ /* ============================== ЧАЙ ============================== */ /* =========================================================================== */ function TeaHarvester() { this.cropName = "plants/tea"; // Имя кропа который нужно харвестить this.cropSeedName = "invobjs/seed-tea"; // Имя семечки которую нужно сажать this.cropStage = 3; // Стадия обработки this.leaveName = "invobjs/tea-fresh"; this.containerID = 0; } TeaHarvester.prototype.processHarvestedGoods = function() { bagsSeedsSorter(this); }; TeaHarvester.prototype.selectObjects = function() { var cnt = jSelectObject("Select container (chest/lchest)..."); if(cnt.name().indexOf("cclosed") == -1 && cnt.name().indexOf("lchest") == -1) { jToConsole("ERROR: This is not a container!"); return false; } else this.containerID = cnt; return true; } /* =========================================================================== */ /* ============================== МАК ============================== */ /* =========================================================================== */ function PoppyHarvester() { this.cropName = "plants/poppy"; // Имя кропа который нужно харвестить this.cropSeedName = "invobjs/seed-poppy"; // Имя семечки которую нужно сажать this.cropStage = 4; // Стадия обработки } PoppyHarvester.prototype.processHarvestedGoods = function() { bagsSeedsSorter(this); }; /* =========================================================================== */ /* ============================== ПШЕНИЧКА ============================== */ /* =========================================================================== */ function WheatHarvester() { this.cropName = "plants/wheat"; // Имя кропа который нужно харвестить this.cropSeedName = "wheat"; // Имя семечки которую нужно сажать this.cropStage = 3; // Стадия обработки } WheatHarvester.prototype.processHarvestedGoods = function() { bagsSeedsSorter(this); }; /*==============================Кропчики============================================*/ function bagsSeedsSorter(harv) { inventory = checkInventory(); var bags = inventory.getItems("invobjs/bag-seed"); var free = inventory.freeSlots(); var fslots = 0; if (bags.length > 0) { var windows = jGetWindows("Seedbag"); if (windows.length > 0) { var seeds = []; var maxseedscount = bags.length * 9; //getting seeds from all seedbags into array for (var i = 0; i < windows.length; i++) { var inv = waitInventory(windows[i], 0); fslots += inv.freeSlots(); var bufseeds = inv.getItems(harv.cropSeedName); for (var j = 0; j < bufseeds.length; j++) { seeds.push(bufseeds[j]); } } //from inventory to same array var invseeds = inventory.getItems(harv.cropSeedName); for (var j = 0; j < invseeds.length; j++) { seeds.push(invseeds[j]); } //calculation quality of all seeds var allq = 0; for (var j = 0; j < seeds.length; j++) { allq += seeds[j].quality(); } //medium quality var mediumrange = (allq / seeds.length) - 1; //correcting medium q //too many seeds if(fslots < 9) mediumrange += 1; if (seeds.length > maxseedscount * 0.75) { for (var j = 0; j < seeds.length; j++) { if (seeds[j].quality() <= mediumrange) seeds[j].drop(); } organizeBags(inventory, harv.cropSeedName); } }//bags windows } if (inventory.freeSlots() < 4 && bags.length < 1) { inventory.sortItems(inventory.getItems(harv.cropSeedName), "quality", false); for (i = 0; i < 3 - free; i++) { if (seeds[i].isActual()) { seeds[i].drop(); } } } } function organizeBags(inv, name) { var bagw = jGetWindows("Seedbag"); if(bagw.length < 1) return; var fullb, nextb; fullb = 0; nextb = bagw.length - 1; while(1) { if(fullb == nextb) break; //breaks if filling bag == last bag if(bagw[fullb].getInventories()[0].freeSlots() == 0) { fullb++; continue; } if(bagw[nextb].getInventories()[0].freeSlots() == 9) { nextb--; continue; } var freeSlot = bagw[fullb].getInventories()[0].freeSlotsCoords()[0]; //getting 1st empty slot at current filling bag var seed = bagw[nextb].getInventories()[0].getItems(name)[0]; //getting 1st seed at current emptying bag seed.take(); jWaitDrag(); bagw[fullb].getInventories()[0].drop(freeSlot); //drops to filling bag jWaitDrop(); } } function openAllSeedbags() { inv = checkInventory(); var bags = inv.getItems("invobjs/bag-seed"); if(bags.length < 1) return; for(var i = 0; i < bags.length; i++) if(bags[i].isActual()) bags[i].iact(); while(jGetWindows("Seedbag").length != bags.length) jSleep(300); } function waitUnActual(item) { while (item.isActual()) { jSleep(100); } } function resetCursor() { if (!jIsCursor("arw")) { jAbsClick(jCoord(0, 0), 3, 0); jWaitCursor("arw", actionTimeout); } } function checkInventory() { if(!jHaveWindow("Inventory")) { jToggleInventory(); while(!jHaveWindow("Inventory")) jSleep(100); } return jGetWindow("Inventory").getInventories()[0]; } function checkEquipment() { if(!jHaveWindow("Equipment")) { jToggleEquipment(); while(!jHaveWindow("Equipment")) jSleep(100); } return jGetJSEquip(); } function waitInventoryObject(inv, objname) { while (true) { var objs = inv.getItems(objname); if (objs.length > 0) break; else jSleep(100); } } function waitUnActual(item) { while (item.isActual()) { jSleep(100); } } function drinkWater() { if (jGetStamina() > 80) return; inventory = checkInventory(); var buckets = inventory.getItems("bucket-water"); if (buckets.length > 0) { inventory.sortItems(buckets, "amount", false); var bucket = buckets[0]; var bucket_coord = bucket.coord(); if (bucket.isActual()) { bucket.take(); jWaitDrag(); var flasks = inventory.getItems("waterflask", "waterskin"); if (flasks.length > 0) { var flask = flasks[0]; if (flask.isActual()) { flask.itemact(0); jSleep(500); inventory.drop(bucket_coord); jWaitDrop(); } } } } var flasks = inventory.getItems("waterflask", "waterskin"); if (flasks.length > 0) { var flask = flasks[0]; if (flask.isActual()) { flask.iact(); if (jWaitPopup(actionTimeout)) { jSelectContextMenu("Drink"); jWaitProgress(); } else { // No water stopFlag = true; } } } } function TileInfo(crd) { this.coord = crd; } function findCrop(harv) { var objid = 0; for (var i = 0; i < cropRadius; i++) { objid = (jFindObjectWithOffset(harv.cropName, 1, jCoord(0, i))); // down if (objid && objid.blob(0) == harv.cropStage) return objid; } for (var i = 0; i < cropRadius; i++) { objid = jFindObjectWithOffset(harv.cropName, 1, jCoord(0, -i)); // up if (objid && objid.blob(0) == harv.cropStage) return objid; } for (var i = 0; i < cropRadius; i++) { objid = jFindObjectWithOffset(harv.cropName, 1, jCoord(i, 0)); // right if (objid && objid.blob(0) == harv.cropStage) return objid; } for (var i = 0; i < cropRadius; i++) { objid = jFindObjectWithOffset(harv.cropName, 1, jCoord(-i, 0));// left if (objid && objid.blob(0) == harv.cropStage) return objid; } objid = jFindObjectByName(harv.cropName, cropRadius); if (objid.blob(0) != harv.cropStage) objid = -1; return objid; } function dropItem(coord) { var items = inventory.getItems(""); for (var i = 0; i < items.length; i++) { if (items[i].coord().x == coord.x && items[i].coord().y == coord.y) { items[i].drop(); break; } } } function waitDragName(name) { while (true) { var item = jGetDraggingItem(); if (item != null) { if (item.resName() != null) { if (item.resName().indexOf(name) >= 0) { break; } else { jSleep(100); } } else jSleep(100); } else { break; } } } function equipScythe() { var equip = checkEquipment(); var scythe = inventory.getItems("scythe")[0]; if (!scythe) return; if (equip.resName(6).indexOf("shovel") >= 0) { equip.takeAt(6); jWaitDrag(actionTimeout); inventory.drop(scythe.coord()); jSleep(1000); waitDragName("scythe"); equip.dropTo(6); jWaitDrop(actionTimeout); } } function equipShovel() { // From scythe var equip = checkEquipment(); var shovel = inventory.getItems("shovel")[0]; if (!shovel) return; if (equip.resName(6).indexOf("scythe") >= 0) { dropItem(shovel.coord().add(0, 2)); equip.takeAt(6); jWaitDrag(actionTimeout); inventory.drop(shovel.coord()); jSleep(1000); waitDragName("shovel"); equip.dropTo(6); jWaitDrop(actionTimeout); } } function plowTile() { drinkWater(); equipShovel(); jSendAction("plow"); jWaitCursor("dig", actionTimeout); jOffsetClick(jCoord(0, 0), 1, 0); jWaitProgress(); jOffsetClick(jCoord(0, 0), 3, 0); jWaitCursor("arw", actionTimeout); } function sayArea(text) { var chats = jGetChats(); for (var i = 0; i < chats.length; i++) { if (chats[i].chatName().indexOf("Area Chat") >= 0) { chats[i].sendMessage(text); break; } } } function sortFunction2(a, b) { if (!a.isActual() || !b.isActual()) return 0; else return a.quality() - b.quality(); } function sortFunction(a, b) { if (!a.isActual() || !b.isActual()) return 0; if (a.quality() < b.quality()) return 1; if (a.quality() > b.quality()) return -1; return 0; } function waitInventory(wnd, index) { while (wnd.getInventories().length <= 0) { jSleep(100); } return wnd.getInventories()[index]; } function plantNewCrop(harv, notrecursive) { jSleep(300); inventory = checkInventory(); var plantedSeed = null; var windows = jGetWindows("Seedbag"); var seedbags = inventory.getItems("invobjs/bag-seed-f"); if (seedbags.length > 0) { var seeds = []; for (var i = 0; i < windows.length; i++) { var inv = waitInventory(windows[i], 0); if (inv) { var bufseeds = inv.getItems(harv.cropSeedName); for (var j = 0; j < bufseeds.length; j++) { seeds.push(bufseeds[j]); } } } var invseeds = inventory.getItems(harv.cropSeedName); for (var j = 0; j < invseeds.length; j++) { seeds.push(invseeds[j]); } var sorted_seeds = seeds.sort(sortFunction); plantedSeed = sorted_seeds[0]; } else { var invseeds = inventory.getItems(harv.cropSeedName); if (invseeds.length > 0) { inventory.sortItems(invseeds, "quality", true); plantedSeed = invseeds[0]; } } if (plantedSeed) { // Plow tile if needed if (jGetTileType(jCoord(0, 0)) != 9) { plowTile(); } if (plantedSeed.isActual()) { plantedSeed.take(); jWaitDrag(); jInteractClick(jCoord(0, 0), 0); if (!jWaitDrop(cannotPlantTimeout)) { jWaitDrop(); if (notrecursive) return; plowTile(); plantNewCrop(harv, true); } } else { if (!notrecursive) plantNewCrop(harv, true); else jPrint("Double fail on non-actual seed"); } } } function waitPFEndMove() { while (true) { jWaitEndMove(); jSleep(500); if (!jIsMoving()) { return; } } } function main() { jDropLastWindow(); openAllSeedbags(); var plist = ["gfx/terobjs/plants/carrot", "gfx/terobjs/plants/poppy", "gfx/terobjs/plants/wheat", "gfx/terobjs/plants/hemp", "gfx/terobjs/plants/beetroot", "gfx/terobjs/plants/wine", "gfx/terobjs/plants/tea", "gfx/terobjs/plants/flax", "gfx/terobjs/plants/pepper"]; var pselect = jSelectObject("Select plant"); var pns = pselect.name().toString(); jToConsole("Plant name = "+pns); if(pns == plist[0]) harvester = new CarrotHarvester(); if(pns == plist[1]) harvester = new PoppyHarvester(); if(pns == plist[2]) harvester = new WheatHarvester(); if(pns == plist[3]) harvester = new HempHarvester(); if(pns == plist[4]) harvester = new BeetHarvester(); if(pns == plist[5]) harvester = new WineHarvester(); if(pns == plist[6]) { harvester = new TeaHarvester(); if(!harvester.selectObjects()) return; } if(pns == plist[7]) harvester = new FlaxHarvester(); if(pns == plist[8]) harvester = new PepperHarvester(); while (!stopFlag) { var crop = findCrop(harvester); if (crop == -1) break; // No crops found drinkWater(); equipScythe(); if (!jIsPathFree(crop.position())) { resetCursor(); jPFMove(crop.position()); jWaitStartMove(); waitPFEndMove(); while (jIsMoving() || jMyCoords().dist(crop.position()) > 1) { jSleep(100); } } jSendAction("harvest"); jWaitCursor("harvest", actionTimeout); jDoClick(crop.getID(), 1, 0); jWaitProgress(); harvester.processHarvestedGoods(); plantNewCrop(harvester, false); } } main();