private void Run() { Map.TileCollection tiles = null; Objects.Map.Tile corpseTile = null; Objects.Container lootContainer = null; Loot[] loots = null; Random rand = new Random(); this.OpenCorpseCalled += new OpenCorpseCalledHandler(delegate(Map.TileCollection tileCollection, Objects.Map.Tile tile) { tiles = tileCollection; corpseTile = tile; this.ResetEvent.Set(); }); this.LootItemsCalled += new LootItemsCalledHandler(delegate(Objects.Container container, IEnumerable <Loot> loot) { lootContainer = container; loots = loot.ToArray(); this.ResetEvent.Set(); }); // signal parent thread that this thread is ready this.ResetEventMultiUse.Set(); while (true) { try { this.Cancel = false; this.ResetEvent.Wait(); #region open corpse if (corpseTile != null) { while (!this.Cancel) { this.ResetEventMultiUse.WaitOne(); if (this.Cancel || corpseTile == null) { break; } if (!this.Parent.Client.Player.Location.IsOnScreen(corpseTile.WorldLocation)) { break; } if (this.Parent.Client.Player.IsWalking) { continue; } Map.TileObject topItem = corpseTile.GetTopUseItem(false); if (topItem == null || !topItem.HasFlag(Enums.ObjectPropertiesFlags.IsContainer)) { break; } Objects.Map.Tile playerTile = this.CachedTiles.GetTile(count: this.Parent.Client.Player.ID); if (playerTile == null) { break; } if (playerTile.WorldLocation.DistanceTo(corpseTile.WorldLocation) >= 2) { int distance = int.MaxValue; Objects.Map.Tile bestTile = corpseTile; foreach (Objects.Map.Tile tile in this.CachedTiles.GetAdjacentTileCollection(corpseTile).GetTiles()) { if (!tile.IsWalkable()) { continue; } var pathToTile = playerTile.WorldLocation.GetTilesToLocation(this.Parent.Client, tile.WorldLocation, this.CachedTiles, this.Parent.PathFinder, true).ToArray(); if (pathToTile.Length == 0) { continue; } if (distance > pathToTile.Length) { distance = pathToTile.Length; bestTile = tile; } } if (bestTile == corpseTile && !playerTile.WorldLocation.CanReachLocation(this.Parent.Client, bestTile.WorldLocation)) { break; } this.Parent.Client.Player.GoTo = bestTile.WorldLocation; continue; } for (int i = 0; i < 2; i++) { if (this.Cancel) { break; } Objects.Container container = this.Parent.Client.Inventory.GetContainer(this.Parent.Client.Inventory.GetClosedContainerNumber()); topItem.Use(); for (int j = 0; j < 5; j++) { Thread.Sleep(100); if (container.IsOpen) { break; } } if (!container.IsOpen) { continue; } if (this.CorpseOpened != null) { this.CorpseOpened(container); } break; } break; } corpseTile = null; } #endregion #region loot item(s) if (lootContainer != null) { if (!lootContainer.IsOpen || lootContainer.ItemsAmount == 0) { if (!this.Cancel && this.LootingFinished != null) { this.LootingFinished(lootContainer); } lootContainer = null; continue; } this.LootItems(lootContainer, loots, tiles); if (!this.Cancel && this.LootingFinished != null) { this.LootingFinished(lootContainer); } lootContainer = null; loots = null; } #endregion this.ResetEvent.Reset(); } catch (Exception ex) { if (this.ErrorOccurred != null) { this.ErrorOccurred(ex); } } } }
public static void Main(Client client) { Location offset = new Location(1, 0, 0), // fixed offset of loot pile finishedContainerOffset = new Location(0, 0, 0); // fixed offset of finished container bool useOffset = false, // whether to only check on a location specified by offset useNestedContainers = true, // whether to dump into nested containers openContainersOnGround = true; // whether to use containers on the ground as loot containers Container lootContainer = null, finishedContainer = null; Location lootContainerLocation = null; int fullIndex = int.MaxValue; string statusBarFull = "You cannot put more objects"; while (true) { Thread.Sleep(50); if (!client.Player.Connected) { continue; } if (openContainersOnGround) { if (lootContainerLocation != null && lootContainer != null && lootContainer.IsOpen && finishedContainer != null && finishedContainer.IsOpen) { if (fullIndex <= 0) { lootContainer.Close(); fullIndex = int.MaxValue; var top = client.Map.GetTopMoveItem(lootContainerLocation); if (top != null && top.HasFlag(Enums.ObjectPropertiesFlags.IsContainer)) { var best = finishedContainer.GetBestSlot(top.ToItemLocation()); if (best != null) { top.Move(best); Thread.Sleep(500); } } lootContainer = null; lootContainerLocation = null; } } // find loot container if (lootContainer == null || !lootContainer.IsOpen) { lootContainer = null; foreach (var tile in client.Map.GetAdjacentTiles(client.Player.Location).GetTiles()) { if (tile.WorldLocation == client.Player.Location.Offset(finishedContainerOffset)) { continue; } var top = tile.GetTopUseItem(false); if (top == null || !top.HasFlag(Enums.ObjectPropertiesFlags.IsContainer)) { continue; } for (int i = 0; i < 3; i++) { lootContainer = top.TryOpen(); if (lootContainer != null) { break; } } if (lootContainer == null) { continue; } lootContainerLocation = tile.WorldLocation; break; } // check if container wasn't found // exit script if so if (lootContainer == null) { return; } } // find finished container if (finishedContainer == null || !finishedContainer.IsOpen) { finishedContainer = null; var top = client.Map.GetTopUseItem(client.Player.Location.Offset(finishedContainerOffset), false); if (top == null || !top.HasFlag(Enums.ObjectPropertiesFlags.IsContainer)) { return; } for (int i = 0; i < 3; i++) { finishedContainer = top.TryOpen(); if (finishedContainer != null) { break; } } if (finishedContainer == null) { return; } } } else { // assign loot container to any open container that has free slots foreach (var container in client.Inventory.GetContainers()) { if (container.IsFull) { continue; } lootContainer = container; break; } // check if container wasn't found // wait and try again if so if (lootContainer == null) { continue; } } Map.TileObject topitem = null; if (!useOffset) { // find loot pile foreach (var tile in client.Map.GetAdjacentTiles(client.Player.Location).GetTiles()) { if (openContainersOnGround) { var loc = tile.WorldLocation; if (loc == client.Player.Location.Offset(finishedContainerOffset) || loc == lootContainerLocation) { continue; } } var top = tile.GetTopMoveItem(); if (top == null || !top.HasFlag(Enums.ObjectPropertiesFlags.IsPickupable) || top.HasFlag(Enums.ObjectPropertiesFlags.IsContainer)) { continue; } topitem = top; break; } } else { var tile = client.Map.GetTile(client.Player.Location.Offset(offset)); if (tile == null) { continue; } if (tile.GetObjects().ToArray().Length <= 1) { break; // tile is empty } var top = tile.GetTopMoveItem(); if (topitem == null || topitem.ID < 100) { return; } if (!topitem.HasFlag(Enums.ObjectPropertiesFlags.IsPickupable)) { return; } topitem = top; } if (topitem == null) { return; } if (!useNestedContainers) { ItemLocation itemLoc = lootContainer.GetBestSlot(topitem.ToItemLocation()); if (itemLoc == null) { continue; } topitem.Move(itemLoc); } else { var items = lootContainer.GetItems().ToArray(); int i = Math.Min(fullIndex, items.Length - 1); while (i >= 0) { if (!lootContainer.IsOpen) { break; } var item = items[i]; if (fullIndex <= i || !item.HasFlag(Enums.ObjectPropertiesFlags.IsContainer)) { i--; continue; } topitem.Move(item.ToItemLocation()); //Thread.Sleep(200); if (client.Window.StatusBar.GetText().StartsWith(statusBarFull)) { fullIndex = i; client.Window.StatusBar.SetText(string.Empty); Thread.Sleep(1000); client.Window.StatusBar.SetText(string.Empty); } break; } } } }