Example #1
0
    public static List <ItemIdCount> ResolveItemsToProcess(List <ItemIdCount> itemsNeedToGet, CSAssembly core = null, CSFactory factory = null)
    {
        List <ItemIdCount> resourceItems  = new List <ItemIdCount> ();
        List <ItemIdCount> ItemsOwnRecord = new List <ItemIdCount> ();

        foreach (ItemIdCount iic in itemsNeedToGet)
        {
            if (CSProcessing.CanProcessItem(iic.protoId))
            {
                resourceItems.Add(iic);
                continue;
            }
            List <ItemIdCount> needReplicate = new List <ItemIdCount> ();
            needReplicate.Add(iic);
            do
            {
                List <ItemIdCount> tempNeed = new List <ItemIdCount> ();
                tempNeed.AddRange(needReplicate);
                foreach (ItemIdCount tempIic in tempNeed)
                {
                    Replicator.KnownFormula[] msList = UIGraphControl.GetReplicator().GetKnowFormulasByProductItemId(tempIic.protoId);
                    if (msList == null || msList.Length == 0)
                    {
                        needReplicate.Remove(tempIic);
                        //Debug.LogError("can't get "+tempIic.protoId+"for colony");
                        continue;
                    }
                    //--to do: temp_ only use script 01
                    Replicator.Formula ms = Replicator.Formula.Mgr.Instance.Find(msList[0].id);
                    foreach (Replicator.Formula.Material mt in ms.materials)
                    {
                        int needCount = mt.itemCount * Mathf.CeilToInt(tempIic.count * 1.0f / ms.m_productItemCount);
                        if (core != null)
                        {
                            int ownMaterialCount = CSUtils.GetItemCountFromAllStorage(mt.itemId, core);
                            if (factory != null)
                            {
                                ownMaterialCount += factory.GetAllCompoundItemCount(mt.itemId);
                            }

                            ItemIdCount ownRecord = ItemsOwnRecord.Find(it => it.protoId == mt.itemId);
                            if (ownMaterialCount > 0)
                            {
                                if (ownRecord == null || ownRecord.count < ownMaterialCount)
                                {
                                    int leftRecordCount = 0;
                                    if (ownRecord == null)
                                    {
                                        leftRecordCount = ownMaterialCount;
                                    }
                                    else
                                    {
                                        leftRecordCount = ownMaterialCount - ownRecord.count;
                                    }
                                    int addRecordCount = 0;
                                    if (needCount > leftRecordCount)
                                    {
                                        needCount     -= leftRecordCount;
                                        addRecordCount = leftRecordCount;
                                    }
                                    else
                                    {
                                        needCount      = 0;
                                        addRecordCount = needCount;
                                    }
                                    if (ownRecord == null)
                                    {
                                        ItemsOwnRecord.Add(new ItemIdCount(mt.itemId, addRecordCount));
                                    }
                                    else
                                    {
                                        ownRecord.count += addRecordCount;
                                    }
                                    if (needCount == 0)
                                    {
                                        continue;
                                    }
                                }
                            }
                        }

                        if (CSProcessing.CanProcessItem(mt.itemId))
                        {
                            CSUtils.AddItemIdCount(resourceItems, mt.itemId, needCount);
                        }
                        else
                        {
                            CSUtils.AddItemIdCount(needReplicate, mt.itemId, needCount);
                        }
                    }
                    needReplicate.Remove(tempIic);
                }
            }while(needReplicate.Count > 0);
        }

        return(resourceItems);
    }