private IEnumerator BeginAI() { while (true) { switch (Random.Range(0, 2)) { case 0: // Offer a trade. var info = MetaInformation.Instance(); if (info != null) { int numMappings = info.GetNumberOfRegisteredItems(); if (numMappings != 0) { ItemType offType = info.GetItemTypeMappings().ElementAt(Random.Range(0, numMappings)).Value; ItemType reqType = info.GetItemTypeMappings().ElementAt(Random.Range(0, numMappings)).Value; int numOfferedItem = Random.Range(1, 10); int numRequestItem = Random.Range(1, 10); TradingOffer offer = TradingOffer.MakeOffer( new ItemStack(offType, numOfferedItem), new ItemStack(reqType, numRequestItem)); bool didSucceed = false; yield return(TradingDialogUtility.OfferTrade(offer, (result) => { didSucceed = result == TradingResult.SUCCEED; })); if (didSucceed) { NotificationSystem.ShowNotificationIfPossible("Trade succeeded."); } else { NotificationSystem.ShowNotificationIfPossible("Trade failed."); } break; } else { goto case 1; } } else { Debug.Log("No MetaInformation instance found. AI will not make a random trade offer."); goto case 1; } case 1: // Move to a random piece of furniture in the shop. Shop shop = myShopMover.GetShop(); Furniture target = shop.GetFurnitureAtIndex(Random.Range(0, shop.GetFurnitureAmount())); yield return(myShopMover.MoveToPosition(target.GetStandingPosition(), (succ) => {})); break; } } }
private IEnumerator BasicCustomerRoutine() { Game game = Game.current; if (game == null) { Debug.Log("Game.current is null; Customer won't do anything."); yield break; } // Walk to a random furniture. bool succeededMovingIntoShop = false; while (!succeededMovingIntoShop) { var allFurniture = game.furnitureInShop; if (allFurniture.Count > 0) { IntPair randomFurniturePosition = allFurniture [Random.Range(0, allFurniture.Count)].furnitureRef.GetStandingPosition(); yield return(myShopMover.MoveToPosition(randomFurniturePosition, (suc) => succeededMovingIntoShop = suc)); } else { yield return(myShopMover.MoveToPosition(new IntPair(0, 0))); succeededMovingIntoShop = true; } if (!succeededMovingIntoShop) { yield return(new WaitForSeconds(0.1f)); // to prevent rapid looping } } // Pop up a trading dialog. var possibleOffers = character.possibleTradingOffers.Where((off) => Game.current.inventory.HasItem(off.Request.ItemType)).ToList(); if (possibleOffers.Count <= 0) { Debug.Log("No offers possible."); } else { TradingOffer trade = possibleOffers [Random.Range(0, possibleOffers.Count)]; string myTradingText; if (character.formattedTradingStrings.Length > 0) { var strs = character.formattedTradingStrings; var idx = Random.Range(0, strs.Length); myTradingText = string.Format(strs [idx], trade.Offer.ItemType.Name, trade.Request.ItemType.Name, trade.Offer.Count, trade.Request.Count); } else { myTradingText = "UNIMPLEMENTED TRADING TEXT"; } yield return(TradingDialogUtility.OfferTrade(trade, myTradingText, (result) => { switch (result) { case TradingResult.SUCCEED: NotificationSystem.ShowNotificationIfPossible(string.Format("Trade succeeded! You got {0} {1}.", trade.Offer.Count, trade.Offer.ItemType.Name)); break; case TradingResult.FAILACCEPT: NotificationSystem.ShowNotificationIfPossible(string.Format("Couldn't do the trade. You didn't have enough {0}", trade.Request.ItemType.Name)); break; case TradingResult.FAILDENY: NotificationSystem.ShowNotificationIfPossible("Declined trade."); break; } })); } // Return to start position. yield return(myShopMover.MoveToPosition(startPosition, (suc) => {})); // Despawn self! Destroy(gameObject); }