/// <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); }
public EntityGroupVillageTrader(VillageTraderTask task, EntityGroup.GroupType type, List <Entity> entities = null) : base(task.Start.Settlement.BaseChunk, entities, task.ToSell) { Task = task; HasSold = false; Type_ = type; //We create a path to the target settlement GenerateNewPath(task.End.Settlement.BaseChunk); }
private bool HasEntityGroupOfType(EntityGroup.GroupType type) { foreach (EntityGroup g in CurrentActiveGroups) { if (g.Type == type) { return(true); } } return(false); }
/// <summary> /// Attempts to export produce for a village. /// We first check if a group exporting this produce type exists /// If so, then we do not send another /// If not, then we remove the exported items from the settlement inventory, and add /// them to the traders inventory. We then send this data to /// </summary> private void VillageExport(EconomicItemType type) { EntityGroup.GroupType groupType = type.GetTypeFromExport(); if (HasEntityGroupOfType(groupType)) { return; } EconomicInventory traderInventory = new EconomicInventory(); //We iterate the surplus and add any relevent items to the inventory foreach (KeyValuePair <EconomicItem, int> surplus in Surplus) { //If the surplus item is of correct type if (surplus.Key.ExportType == type) { //We add to the trader inventory, remove it from the settlement inventory traderInventory.AddItem(surplus.Key, surplus.Value); Inventory.RemoveItem(surplus.Key, surplus.Value); //And then finally remove from the surplus //Surplus.Remove(surplus.Key); } } //Create the group //TODO - decide which entities will form the group EntityGroup group = WorldEventManager.Instance.SpawnVillageTrader(this, type, traderInventory, null); if (group != null) { Debug.Log("Group of type " + group.Type + " exporting from " + Settlement); } else { Debug.Log("Null group"); } if (group == null) { Inventory.AddAll(traderInventory.GetAllItems()); } else { CurrentActiveGroups.Add(group); foreach (KeyValuePair <EconomicItem, int> kvp in traderInventory.GetAllItems()) { Surplus.Remove(kvp.Key); } } }