Пример #1
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);
            }
        }
    }
    public override bool OnReachDestination(Vec2i position)
    {
        //If we haven't sold yet, then we are at our desitnation and must sell produce
        if (!HasSold)
        {
            Debug.Log("Village trader has sold");
            //We sell all items to the settlement
            int income = Task.End.Import(EconomicInventory.GetAllItems());

            //We then iterate each desired purchase and attempt to buy from settlement
            foreach (KeyValuePair <EconomicItem, int> desiredPurch in Task.DesiredPurchases)
            {
                //We attempt to buy
                if (Task.End.AttemptExport(desiredPurch.Key, desiredPurch.Value, income, out float remainingMoney, out int totalPurched))
                {
                    EconomicInventory.AddItem(desiredPurch.Key, totalPurched);
                    income = (int)remainingMoney;
                }
            }
            Debug.Log("Finding path back home");
            //After we have sold, we set the path as heading back to the home settlement.
            GenerateNewPath(Task.Start.Settlement.BaseChunk);
            HasSold = true;
            return(true);
        }
        else
        {
            Debug.Log("back home");
            //if we have reached the destination now, we are back at our home settlement
            foreach (var kvp in EconomicInventory.GetAllItems())
            {
                Task.Start.Inventory.AddItem(kvp.Key, kvp.Value);
            }

            //As the task is complete, this group should be destoryed
            ShouldDestroy = true;
            Task.Start.EntityGroupReturn(this);
            return(false);
        }
    }
Пример #3
0
    /// <summary>
    /// Updates the settlement economy for this tick.
    /// First adds all raw production to the settlement inventory,
    /// we then subtract all the required use per tick
    /// Then attempts to run all productions available, taking from the inventory
    /// its produce will then be added to the inventory
    /// </summary>
    public void Tick()
    {
        //We first iterate all the raw production and add to inventory
        foreach (KeyValuePair <EconomicItem, int> kvp in RawProductionPerTick)
        {
            Inventory.AddItem(kvp.Key, kvp.Value);
        }
        //We then interate all the use per tick andsubtract from the inventory
        foreach (KeyValuePair <EconomicItem, int> kvp in UsePerTick)
        {
            int rem = Inventory.RemoveItem(kvp.Key, kvp.Value);

            if (rem < 0)
            {
                // Debug.Log(World.Instance.GetSettlement(SettlementID).ToString() + " has run out of " +  kvp.Key);
                // Debug.Log("RUN OUT OF " + kvp.Key);
            }
        }

        //We iterate all the productions, and calculate their productions
        foreach (KeyValuePair <EconomicProduction, int> prod in EconomicProduction)
        {
            CalculateEconomicProduction(prod.Key, prod.Value);
        }

        Surplus.Clear();
        SurlusByExportType.Clear();
        SurplusByTypeCount.Clear();
        //We order our surplus by item type - ready for exports

        int surplusCount = 0;

        RequiredImports.Clear();
        //We iterate all the items in our inventory
        foreach (KeyValuePair <EconomicItem, int> allItems in Inventory.GetAllItems())
        {
            int itemCount = allItems.Value;
            //If this item has a desired stock level
            if (DesiredItemStock.TryGetValue(allItems.Key, out int desiredStock))
            {
                //We calculate our surplus
                int surplus = itemCount - desiredStock;
                //if negative, then we add to required imports
                if (surplus < 0)
                {
                    if (!RequiredImports.ContainsKey(allItems.Key))
                    {
                        RequiredImports.Add(allItems.Key, 0);
                    }
                    RequiredImports[allItems.Key] -= surplus;
                }
                else
                {//if positive, we add to surplus
                    if (!Surplus.ContainsKey(allItems.Key))
                    {
                        Surplus.Add(allItems.Key, 0);
                    }
                    Surplus[allItems.Key] += surplus;
                    surplusCount          += surplus;

                    //We check if we already have included a surplus of this export type
                    if (!SurplusByTypeCount.ContainsKey(allItems.Key.ExportType))
                    {
                        SurplusByTypeCount.Add(allItems.Key.ExportType, 0);
                    }

                    SurplusByTypeCount[allItems.Key.ExportType] += surplus;
                }
            }
            else
            {
                if (!Surplus.ContainsKey(allItems.Key))
                {
                    Surplus.Add(allItems.Key, 0);
                }
                //We check if we already have included a surplus of this export type
                if (!SurplusByTypeCount.ContainsKey(allItems.Key.ExportType))
                {
                    SurplusByTypeCount.Add(allItems.Key.ExportType, 0);
                }

                Surplus[allItems.Key] += allItems.Value;
                SurplusByTypeCount[allItems.Key.ExportType] += allItems.Value;
            }
        }

        //If this settlement is a village, then we desire to export any surplus produce to near by places
        if (Settlement.SettlementType == SettlementType.VILLAGE)
        {
            int foodSurplus   = 0;
            int animalSurplus = 0;
            int oreSurplus    = 0;
            int woodSurplus   = 0;
            SurplusByTypeCount.TryGetValue(EconomicItemType.food, out foodSurplus);
            SurplusByTypeCount.TryGetValue(EconomicItemType.animalProduce, out animalSurplus);
            SurplusByTypeCount.TryGetValue(EconomicItemType.ore, out oreSurplus);
            SurplusByTypeCount.TryGetValue(EconomicItemType.wood, out woodSurplus);

            if (foodSurplus > 2000)
            {
                VillageExport(EconomicItemType.food);
            }
            if (animalSurplus > 1000)
            {
                VillageExport(EconomicItemType.animalProduce);
            }
            if (oreSurplus > 1000)
            {
                VillageExport(EconomicItemType.ore);
            }


            if (woodSurplus > 1000)
            {
                VillageExport(EconomicItemType.wood);
            }
        }
        //if this settlemet is a village, then we check how much surplus stuff we have
    }