Ejemplo n.º 1
0
    public bool CanImport(EconomicItemType type)
    {
        //Village imports nothing
        if (Settlement.SettlementType == SettlementType.VILLAGE)
        {
            return(false);
        }
        if (type == EconomicItemType.food)
        {
            return(true);
        }

        if (type == EconomicItemType.animalProduce)
        {
            return(ProductionAbility.HasButcher);
        }
        if (type == EconomicItemType.ore)
        {
            return(ProductionAbility.HasSmelter);
        }
        if (type == EconomicItemType.wood)
        {
            return(ProductionAbility.HasLumberMill);
        }

        return(false);
    }
Ejemplo n.º 2
0
    /// <summary>
    /// Searches all settlements in the world, and returns the nearest settlement
    /// that will accept the specified import
    /// </summary>
    /// <param name="start">The settlement that we are exporting from - find closest valid settlement to this</param>
    /// <param name="exportType">The type of economic item we wish our target settlement to import</param>
    /// <returns></returns>
    private SettlementEconomy GetNearestImportingSettlement(Settlement start, EconomicItemType exportType)
    {
        Settlement target      = null;
        int        closestDist = -1;

        //We iterate all settlements in the world
        foreach (KeyValuePair <int, WorldLocation> locs in World.Instance.WorldLocations)
        {
            //if this location is not a settlement
            if (!(locs.Value is Settlement))
            {
                continue;
            }
            Settlement set = locs.Value as Settlement;
            //If the settlement cannot import the required type, we will ignore it
            if (!set.Economy.CanImport(exportType))
            {
                continue;
            }
            //Find the distance from the start to this settlement
            int sqrDist = set.BaseChunk.QuickDistance(start.BaseChunk);

            if (closestDist == -1 || sqrDist < closestDist)
            {
                target      = set;
                closestDist = sqrDist;
            }
        }
        if (target == null)
        {
            return(null);
        }
        return(target.Economy);
    }
Ejemplo n.º 3
0
 public EconomicItem(string name, int id, float value, EconomicItemType type)
 {
     Name       = name;
     ItemID     = id;
     Value      = value;
     ExportType = type;
 }
Ejemplo n.º 4
0
    /// <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);
    }
Ejemplo n.º 5
0
    /// <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);
            }
        }
    }
Ejemplo n.º 6
0
    /// <summary>
    /// Returns the entity group associated with exports of a specific type
    /// </summary>
    /// <param name="type"></param>
    /// <returns></returns>
    public static EntityGroup.GroupType GetTypeFromExport(this EconomicItemType type)
    {
        switch (type)
        {
        case EconomicItemType.animalProduce:
            return(EntityGroup.GroupType.VillageAnimalExport);

        case EconomicItemType.food:
            return(EntityGroup.GroupType.VillageFoodExport);

        case EconomicItemType.ore:
            return(EntityGroup.GroupType.VillageOreExport);

        case EconomicItemType.wood:
            return(EntityGroup.GroupType.VillageWoodExport);
        }
        return(EntityGroup.GroupType.Traders);
    }