private static void PathToPoint(Creature c, Point pos) { int currentFloor = c.CurrentFloor; Point worldIndex = c.WorldIndex; Block[] blocks = currentFloor >= 0 ? Program.WorldMap[worldIndex.X, worldIndex.Y].Dungeon.Floors[currentFloor].Blocks : Program.WorldMap[worldIndex.X, worldIndex.Y].Blocks; int width = Program.WorldMap.TileWidth, height = Program.WorldMap.TileHeight; if (pos.Equals(new Point()) == true) { throw new Exception(); } if (c.Path == null) { List <Point> freeSpots = blocks.GetEmptyAdjacentBlocks(new Point(width, height), pos); Point nextPos = freeSpots.Count > 0 ? freeSpots[Program.RNG.Next(0, freeSpots.Count)] : new Point(); if (!nextPos.Equals(new Point())) { c.SetPath(nextPos); } else { Program.MsgConsole.WriteLine($"The {constructionMap[nextConstruction.X, nextConstruction.Y]} couldn't be built because a path couldn't be found."); constructionMap[nextConstruction.X, nextConstruction.Y] = null; nextConstruction = new Point(); DetermineNextConstruction(); creatureStates[c.ID] = CreatureState.Idle; c.Path = null; } } }
private static void HandleGetMaterial(Creature c, RecipeComponent nextComponent, bool forCrafting = false) { Point nextPos = GetClosestMaterialPos(c, nextComponent, true); // returns new Point() if item is in player's inventory, returns null if the object can't be found // if the material is not on the map or in your inventory if (nextPos == null) { if (nextComponent == RecipeComponent.Log) { HandleChopTree(c); } else if (nextComponent == RecipeComponent.Stone) { HandleMineRock(c); } else { // if there is no crafting recipe stored and the player has a recipe pouch if (currentCraftingRecipe == null && c.Inventory.Exists(i => i is RecipePouch)) { RecipePouch rp = (RecipePouch)c.Inventory.Find(i => i is RecipePouch); foreach (CraftingRecipe r in rp.Recipes) { if (r.CraftingTarget.Exists(e => e.ToComponent() == nextComponent)) { currentCraftingRecipe = r; break; } } } else if (currentCraftingRecipe != null) { if (!currentCraftingRecipe.CraftingTarget.Exists(e => e.ToComponent() == nextComponent)) { currentCraftingRecipe = null; } if (c.Inventory.Exists(i => i.ToComponent() == currentCraftingRecipe.Recipe[0])) { HandleCraftComponent(c); } else { HandleGetMaterial(c, currentCraftingRecipe.Recipe[0], true); } } } return; } if (nextPos.Equals(new Point())) { nextPos = GetClosestMaterialPos(c, nextComponent, false); } if (nextPos == null) { creatureStates[c.ID] = CreatureState.PlaceMaterials; return; } if (!nextPos.Equals(new Point()) || c.CheckCanCarryItem(nextComponent.ToItem()) == true) { bool nextToItem = c.Position.NextToPoint(nextPos); int currentFloor = c.CurrentFloor; Point worldIndex = c.WorldIndex; Block[] blocks = currentFloor >= 0 ? Program.WorldMap[worldIndex.X, worldIndex.Y].Dungeon.Floors[currentFloor].Blocks : Program.WorldMap[worldIndex.X, worldIndex.Y].Blocks; int width = Program.WorldMap.TileWidth, height = Program.WorldMap.TileHeight; if (nextToItem) { if (blocks[nextPos.X * width + nextPos.Y] is Chest chest) { for (int i = chest.Inventory.Count - 1; i >= 0; i--) { if (chest.Inventory[i].ToComponent() == nextComponent) { bool itemAdded = c.AddItem(chest.Inventory[i]); if (itemAdded) { chest.Inventory.RemoveAt(i); } else if (DropUnnecessaryItems(c, nextComponent)) { itemAdded = c.AddItem(chest.Inventory[i]); if (itemAdded) { chest.Inventory.RemoveAt(i); } else { creatureStates[c.ID] = CreatureState.PlaceMaterials; } } else { creatureStates[c.ID] = CreatureState.PlaceMaterials; } } } } else { if (c.CheckCanCarryItem((Item)blocks[nextPos.X * width + nextPos.Y])) { c.GetItem(nextPos); getIndex++; } else { bool droppedItems = DropUnnecessaryItems(c, nextComponent); if (droppedItems == false) { creatureStates[c.ID] = CreatureState.PlaceMaterials; } } } } else if (c.Path == null) { c.SetPath(nextPos); } } else { c.Path = null; creatureStates[c.ID] = CreatureState.PlaceMaterials; } }