예제 #1
0
    void Update()
    {
        if (!PeGameMgr.IsSingle)
        {
            return;
        }
        if (core == null)
        {
            Init();
        }
        if (core == null)
        {
            return;
        }
        counter++;
        if (counter % 450 == 0)
        {
            counter = 0;
            //--to do
            //1.collect requirements
            if (core.Storages == null)
            {
                return;
            }

            CSFactory factory = core.Factory;

            List <CSCommon> storages = core.Storages;
            ClearRequirements();
            List <CSCommon> allCommon = core.GetBelongCommons();
            foreach (CSCommon cse in allCommon)
            {
                if ((cse as CSPPCoal != null) || (cse as CSFarm != null) /*|| (cse as CSMedicalTreat !=null)*/)
                {
                    List <ItemIdCount> requirementsList = cse.GetRequirements();
                    if (requirementsList != null && requirementsList.Count > 0)
                    {
                        if (cse as CSPPCoal != null)
                        {
                            ppcoalRequirements.Add(cse, requirementsList);
                        }
                        //					if(cse as CSMedicalTreat!=null)
                        //						treatRequirements.Add(cse,requirementsList);
                        if (cse as CSFarm != null)
                        {
                            farmRequirements.Add(cse, requirementsList);
                        }
                    }
                }
            }
            bool transferedFactoryItem = false;
            //--to do
            if (storages != null)
            {
                //0.transfer factoryEndItem
                if (factory != null)
                {
                    List <ItemIdCount> replicateEndItem = factory.GetCompoudingEndItem();
                    if (replicateEndItem.Count > 0)
                    {
                        List <int> protoList = CSStorage.GetAutoProtoIdList();
                        foreach (ItemIdCount rIic in replicateEndItem)
                        {
                            if (protoList.Contains(rIic.protoId))
                            {
                                if (CSUtils.AddToStorage(rIic.protoId, rIic.count, core))
                                {
                                    factory.CountDownItem(rIic.protoId, rIic.count);
                                    transferedFactoryItem = true;
                                }
                            }
                        }
                    }
                }

                List <ItemIdCount> requirementsList = storages[0].GetRequirements();
                if (requirementsList != null && requirementsList.Count > 0)
                {
                    storageRequirements.Add(storages[0], requirementsList);
                }
            }

            //2.check doing requirements** one by one? && do requirements
            //1)check storage&factory Get supply
            List <ItemIdCount> itemsNeedToGet = new List <ItemIdCount> ();
            GetItemFromStorageAndFactory(ppcoalRequirements, storages, factory, ref itemsNeedToGet);
            GetItemFromStorageAndFactory(farmRequirements, storages, factory, ref itemsNeedToGet);
            GetItemFromStorageAndFactory(storageRequirements, storages, factory, ref itemsNeedToGet);

            if (itemsNeedToGet.Count > 0)
            {
                //3.analysis to processing or replicate
                //1).check is replicating
                if (factory != null)
                {
                    List <ItemIdCount> itemsInReplicating = new List <ItemIdCount>();
                    List <ItemIdCount> compoundingList    = factory.GetCompoudingItem();
                    foreach (ItemIdCount needItem in itemsNeedToGet)
                    {
                        ItemIdCount cItem = compoundingList.Find(it => it.protoId == needItem.protoId);
                        if (cItem != null)
                        {
                            if (cItem.count >= needItem.count)
                            {
                                itemsInReplicating.Add(new ItemIdCount(cItem.protoId, needItem.count));
                            }
                            else
                            {
                                itemsInReplicating.Add(new ItemIdCount(cItem.protoId, cItem.count));
                            }
                        }
                    }
                    if (itemsInReplicating.Count > 0)
                    {
                        foreach (ItemIdCount ic in itemsInReplicating)
                        {
                            ItemIdCount decreaseItem = itemsNeedToGet.Find(it => it.protoId == ic.protoId);
                            if (decreaseItem.count > ic.count)
                            {
                                decreaseItem.count -= ic.count;
                            }
                            else
                            {
                                itemsNeedToGet.Remove(decreaseItem);
                            }
                        }
                    }

                    //2).put material into storage
                    GetItemMaterialFromFactory(factory, itemsNeedToGet, ref transferedFactoryItem);
                    if (transferedFactoryItem)
                    {
                        ShowTips(ETipType.factory_to_storage);
                    }

                    //3).count what need Processing/ replicate some
                    //1-check can be replicated,if ok, replicate
                    factory.CreateNewTaskWithItems(itemsNeedToGet);
                }
                //2-check the left things to resolve to resource
                //3-in 2,check resource already have, remove it
                List <ItemIdCount> resourceItems = ResolveItemsToProcess(itemsNeedToGet, core, factory);
//				foreach(ItemIdCount iic in resourceItems){
//					int gotCount =0;
//					foreach(CSCommon sc in storages){
//						CSStorage cst = sc as CSStorage;
//						int itemCount = cst.GetItemCount(iic.protoId);
//						if(itemCount>0)
//						{
//							if(itemCount>=iic.count-gotCount)
//							{
//								gotCount = iic.count;
//								break;
//							}else{
//								gotCount += itemCount;
//							}
//						}
//					}
//					iic.count-=gotCount;
//				}

                resourceItems.RemoveAll(it => it.count <= 0);
                if (resourceItems.Count > 0)
                {
                    //3).check is Processing
                    CSProcessing process = core.ProcessingFacility;
                    if (process != null)
                    {
                        List <ItemIdCount> processingList = process.GetItemsInProcessing();
                        foreach (ItemIdCount iic in processingList)
                        {
                            CSUtils.RemoveItemIdCount(resourceItems, iic.protoId, iic.count);
                        }
                    }
                    if (resourceItems.Count > 0)
                    {
                        //4).assign processing task
                        if (process != null)
                        {
                            process.CreateNewTaskWithItems(resourceItems);
                        }
                    }
                }
            }



            //meet desire
            ClearDesires();
            if (storages != null)
            {
                List <ItemIdCount> desireList = storages[0].GetDesires();
                if (desireList != null && desireList.Count > 0)
                {
                    storageDesires.Add(storages[0], desireList);
                    List <ItemIdCount> itemsDesireToGet = new List <ItemIdCount> ();
                    GetItemFromStorageAndFactory(storageDesires, storages, factory, ref itemsDesireToGet);
                    if (itemsDesireToGet.Count > 0 && factory != null)
                    {
                        if (factory != null)
                        {
                            List <ItemIdCount> itemsInReplicating = new List <ItemIdCount>();
                            List <ItemIdCount> compoundingList    = factory.GetCompoudingItem();
                            foreach (ItemIdCount needItem in itemsDesireToGet)
                            {
                                ItemIdCount cItem = compoundingList.Find(it => it.protoId == needItem.protoId);
                                if (cItem != null)
                                {
                                    if (cItem.count >= needItem.count)
                                    {
                                        itemsInReplicating.Add(new ItemIdCount(cItem.protoId, needItem.count));
                                    }
                                    else
                                    {
                                        itemsInReplicating.Add(new ItemIdCount(cItem.protoId, cItem.count));
                                    }
                                }
                            }
                            if (itemsInReplicating.Count > 0)
                            {
                                foreach (ItemIdCount ic in itemsInReplicating)
                                {
                                    ItemIdCount decreaseItem = itemsDesireToGet.Find(it => it.protoId == ic.protoId);
                                    if (decreaseItem.count > ic.count)
                                    {
                                        decreaseItem.count -= ic.count;
                                    }
                                    else
                                    {
                                        itemsDesireToGet.Remove(decreaseItem);
                                    }
                                }
                            }
                        }
                        factory.CreateNewTaskWithItems(itemsDesireToGet);
                    }
                }
            }


            //medical system
            if (core.MedicalCheck != null && core.MedicalTreat != null && core.MedicalTent != null &&
                core.MedicalCheck.IsRunning && core.MedicalTreat.IsRunning && core.MedicalTent.IsRunning &&
                (core.MedicalCheck.WorkerCount + core.MedicalTreat.WorkerCount + core.MedicalTent.WorkerCount > 0))
            {
                if (lastTime < 0)
                {
                    lastTime = GameTime.PlayTime.Second;
                }
                else
                {
                    core.MedicineResearchState += GameTime.PlayTime.Second - lastTime;
                    lastTime = GameTime.PlayTime.Second;
                    if (storages != null && core.MedicineResearchState >= medicineSupplyTime)
                    {
                        Debug.Log("supplyMedicineTime");
                        core.MedicineResearchTimes++;
                        List <ItemIdCount> supportMedicine = new List <ItemIdCount> ();
                        //try supply
                        foreach (MedicineSupply ms in CSMedicineSupport.AllMedicine)
                        {
                            if (core.MedicineResearchTimes - ms.rounds * Mathf.FloorToInt(core.MedicineResearchTimes / ms.rounds)
                                < 1)
                            {
                                if (CSUtils.GetItemCountFromAllStorage(ms.protoId, core) < ItemAsset.ItemProto.GetItemData(ms.protoId).maxStackNum)
                                {
                                    supportMedicine.Add(new ItemIdCount(ms.protoId, ms.count));
                                }
                            }
                        }
                        if (supportMedicine.Count > 0)
                        {
                            if (CSUtils.AddItemListToStorage(supportMedicine, core))
                            {
                                ShowTips(ETipType.medicine_supply);
                            }
                            else
                            {
                                ShowTips(ETipType.storage_full);
                            }
                        }

                        core.MedicineResearchState = 0;
                        if (core.MedicineResearchTimes == Int32.MaxValue)
                        {
                            core.MedicineResearchTimes = 0;
                        }
                    }
                }
            }
            else
            {
                lastTime = GameTime.PlayTime.Second;
            }
        }
    }