/// <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); }