public void OnClick() { if (Group == null) { return; } if (Group.Type == EntityGroup.GroupType.Traders) { EntityGroupCaravan car = Group as EntityGroupCaravan; if (car != null) { string info = string.Format("Caravan is travelling from {0} to {1}, with the following trade: \n", car.StartChunk, car.EndChunk); // Debug.Log(string.Format("Caravan is travelling from {0} to {1}, with the following trade: ", car.StartChunk, car.EndChunk)); foreach (KeyValuePair <EconomicItem, int> kvp in car.Trade) { info += string.Format("{0} : {1} - £{2}\n", kvp.Key, kvp.Value, kvp.Value * kvp.Key.Value); } Debug.Log(info); } EntityGroupVillageTrader farm = Group as EntityGroupVillageTrader; if (farm != null) { string inv = ""; foreach (KeyValuePair <EconomicItem, int> kvp in farm.EconomicInventory.GetAllItems()) { inv += string.Format("{0} - {1}\n", kvp.Key, kvp.Value); } Debug.Log(inv); } } }
/// <summary> /// Spawns an entity group that exports a set type of good from a village, and takes it to the /// nearest valid settlement to sell. /// </summary> /// <param name="home"></param> /// <param name="exportType"></param> /// <param name="export"></param> /// <param name="groupEntities"></param> /// <returns></returns> public EntityGroup SpawnVillageTrader(SettlementEconomy economy, EconomicItemType exportType, EconomicInventory export, List <Entity> groupEntities) { Debug.Log("Spawning village trader"); SettlementEconomy target = null; int nearestDist = -1; //We iterate the near settlements in an attempt to find a settlement to import these goods foreach (int id in economy.NearSettlementsIDs) { Settlement set = World.Instance.GetSettlement(id); if (set == null) { continue; } //We skip other villages if (set.SettlementType == SettlementType.VILLAGE) { continue; } if (!set.Economy.CanImport(exportType)) { continue; } int dist = Vec2i.QuickDistance(economy.Settlement.BaseChunk, set.BaseChunk); if (nearestDist == -1 || dist < nearestDist) { nearestDist = dist; target = set.Economy; } } //If a valid settlement is not found to be close, if (target == null) { //We search all settlements target = GetNearestImportingSettlement(economy.Settlement, exportType); //This should never happen if (target == null) { //in future, the only way this could happen would be if a kingdom cannot trade with any other kingdoms. Debug.LogError("Could not find Settlement to import " + exportType + " near to " + economy.Settlement); return(null); } } EntityGroup.GroupType groupType = exportType.GetTypeFromExport(); VillageTraderTask task = new VillageTraderTask(); task.Start = economy; task.End = target; task.DesiredPurchases = economy.RequiredImports; task.ToSell = export; EntityGroup group = new EntityGroupVillageTrader(task, groupType, groupEntities); AddEntityGroup(group); return(group); }