예제 #1
0
 // Seed Constructor
 public void SeedsConstructor(GameObject mySeed, Soils myAssociatedSoil, string myPlantPrefabName, float myTimeToPlan)
 {
     seed            = mySeed;
     associatedSoil  = myAssociatedSoil;
     plantPrefabName = myPlantPrefabName;
     timeToPlant     = myTimeToPlan;
 }
예제 #2
0
    //function that generates fruits and seeds from a plant if it is happy enough to reproduce, called by growth
    public void DisplayReproduction(int plantHappinessReproduction, Soils soil, int currentCycle)
    {
        //hide mature form if it is the last cycle
        if (currentCycle == fruitCycles)
        {
            if (myMatureForm)
            {
                myMatureForm.GetComponent <Renderer>().enabled = false;
            }
        }

        //Debug.Log("display fruits and seeds");
        //very efficient reproduction phase
        if (plantHappinessReproduction == 2)
        {
            numberOfFruits   = Random.Range(miniFruitHappy, maxFruitHappy);
            numberOfSeeds    = Random.Range(miniSeedHappy, maxSeedHappy);
            numberOfCuttings = Random.Range(miniCuttingHappy, maxCuttingHappy);
        }
        else if (plantHappinessReproduction == 1)
        {
            numberOfFruits   = Random.Range(miniFruitOk, maxFruitOk);
            numberOfSeeds    = Random.Range(miniSeedOk, maxSeedOk);
            numberOfCuttings = Random.Range(miniCuttingHappy, maxCuttingHappy);
        }

        //generation of fruits
        if (numberOfFruits != 0)
        {
            //Debug.Log("we have produced "+ numberOfFruits + " fruits");
            if (fruit)
            {
                for (int i = 0; i < numberOfFruits; i++)
                {
                    float posX = Random.Range(-0.3f, 0.3f) + this.transform.position.x;
                    float posY = Random.Range(0.0f, 0.5f) + this.transform.position.y;
                    float posZ = Random.Range(-0.3f, 0.3f) + this.transform.position.z;
                    temporaryFruit = Instantiate(fruit, new Vector3(posX, posY, posZ), this.transform.rotation);
                    temporaryFruit.transform.SetParent(this.transform);
                    myFruitsList.Add(temporaryFruit);
                }
            }
        }

        //generation of suckers (for plants that produce them)
        if (numberOfSeeds != 0)
        {
            //Debug.Log("we have produced " + numberOfSeeds + " suckers");
            soil.GenerateSuckers(numberOfSeeds, babySucker, this);
        }

        //generation of Internal suckers (for plants that produce them)
        if (numberOfCuttings != 0)
        {
            //Debug.Log("we have produced " + numberOfCuttings + " internal suckers");
            soil.GenerateInternalBabyPlants(numberOfCuttings, babySucker, this);
        }
    }
예제 #3
0
    private void Awake()
    {
        if (instance != null)
        {
            Destroy(gameObject);
        }
        else
        {
            instance = this;
        }

        FindPlant();
    }
예제 #4
0
    // Use this for initialization
    void Start()
    {
        soil = GetComponent <Soils>();

        if (twinklePrefab)
        {
            for (int i = 0; i < 2; i++)
            {
                GameObject go = GameObject.Instantiate(twinklePrefab, transform);
                twinkleObjects.Add(go.GetComponent <TwinkleBehaviour>());
            }
        }
    }
예제 #5
0
    public void OnMouseUp()
    {
        //Debug.Log("Picked up " + itemName + ". " + itemName + " added to inventory.");
        //Debug.Log("associated prefab is" + associatedPrefab.name + "item name is " + itemName);
        inventory.AddToInventoryInGame(itemName, 1);

        //tell the soil to check if there are still baby plants there if we picked a seed
        if (itemType == itemTypes.SEED)
        {
            myParentSoil = this.transform.parent.GetComponent <Soils>();
            myParentSoil.CheckBabyPlants(this.transform);
        }

        Destroy(gameObject);
    }
예제 #6
0
    // Use this for initialization
    void Start()
    {
        //initialTimer = Time.time;

        //get plant data
        myPlantData = this.gameObject.GetComponent <PlantData>();
        myPlantData.SetSpeed();

        initialPlantSize = myPlantData.gameObject.transform.localScale.x;

        //get soil data
        if (SoilOfThePlant)
        {
            mySoils      = SoilOfThePlant.GetComponent <Soils>();
            initialTimer = Time.time - mySoils.mySoilsData.plant_time;
        }
        //Debug.Log("mySoils.mySoilsData.plant_time:" + mySoils.mySoilsData.plant_time);
        //Debug.Log("myPlantData.timeBeforeMature:" + myPlantData.timeBeforeMature+ " myPlantData.timeBeforeFruit:" + myPlantData.timeBeforeFruit + " myPlantData.timeToGatherFruit:" + myPlantData.timeToGatherFruit + " myPlantData.timeBeforeDeath:" + myPlantData.timeBeforeDeath+ " myPlantData.fruitCycles:"+ myPlantData.fruitCycles);

        if (myPlantData && mySoils.mySoilsData.plant_time == 0)
        {
            currentCycle = 1;
            //# this should be improved
            //myPlantData.lifetime = ((myPlantData.timeBeforeMature + myPlantData.timeBeforeFruit + myPlantData.timeToGatherFruit) * myPlantData.fruitCycles + myPlantData.timeBeforeDeath)- mySoils.mySoilsData.plant_time;
            myPlantData.lifetime           = ((myPlantData.timeBeforeMature + myPlantData.timeBeforeFruit + myPlantData.timeToGatherFruit) * myPlantData.fruitCycles + myPlantData.timeBeforeDeath);
            mySoils.mySoilsData.plant_name = myPlantData.plantPrefabName;
        }
        else if (myPlantData && mySoils.mySoilsData.plant_time > 0)
        {
            myPlantData.lifetime           = ((myPlantData.timeBeforeMature + myPlantData.timeBeforeFruit + myPlantData.timeToGatherFruit) * myPlantData.fruitCycles + myPlantData.timeBeforeDeath);
            mySoils.mySoilsData.plant_name = myPlantData.plantPrefabName;
        }
        else
        {
            Debug.Log("No plant data found for " + this.gameObject.name);
        }
        startDone = true;
        //update plant happiness state and related appearance
        InvokeRepeating("UpdatePlantAppearanceHappiness", 0.0f, 1.0f);

        //Debug.Log("current time:" + Time.time + " initialTimer:" + initialTimer+ " myPlantData.lifetime:" + myPlantData.lifetime);
    }
예제 #7
0
    public void myGenerate()
    {
        Debug.Log("begin soil generation");

        //gather the number of soil from the user
        ConnectionInfosObject = GameObject.Find("ConnectionInfos");
        if (ConnectionInfosObject)
        {
            playersData = ConnectionInfosObject.GetComponent <ConnectionInfo>().myPlayersData;
        }
        if (playersData != null)
        {
            numberSoilx = playersData.playerNumbSoilx;
            numberSoily = playersData.playerNumbSoily;

            mySoilsList = new List <Soils>();

            for (int i = 0; i < numberSoilx; i++)
            {
                for (int j = 0; j < numberSoily; j++)
                {
                    //instantiate the soil box at the right position
                    tempPosition       = new Vector3(i, 0, j);
                    myInstantiatedSoil = Instantiate(SoilPrefab, tempPosition, Quaternion.identity);
                    //give it a name
                    myInstantiatedSoil.name = "soil_box_" + i + "_" + j;

                    //add the new soil to the soil list
                    Soils mySoils = myInstantiatedSoil.GetComponent <Soils>();
                    if (mySoils)
                    {
                        mySoils.SoilsConstructor(i, j, myInstantiatedSoil);
                        mySoils.mySoilsData.soil_name = myInstantiatedSoil.name;
                        mySoils.mySoilsData.player_id = playersData.playerId;
                        mySoilsList.Add(mySoils);
                    }
                }
            }
            ConnectionInfosObject.GetComponent <Connecting>().StartOnlineGenerateSoils();
        }
    }
예제 #8
0
 //fonction to create a seed plant, soon to be sucker
 void CreateSeedHere(Soils mySoil, GameObject myBabySucker, PlantData myplantData)
 {
     if (myInventory)
     {
         if (!mySoil.mySoilsData.has_babyplant)
         {
             //Debug.Log("we are creating a baby sucker on "+ mySoil+ " : " + myplantData);
             temporarySucker = Instantiate(myBabySucker, mySoil.transform.position, mySoil.transform.rotation);
             temporarySucker.transform.SetParent(mySoil.transform);
             //add the new seed to the sucker list
             SeedDatas mySeed = temporarySucker.AddComponent <SeedDatas>();
             if (mySeed)
             {
                 //Debug.Log("seed ok, add to list");
                 float timeBeforeSeedReplant = myplantData.lifetime - myplantData.timeBeforeFruit;
                 mySeed.SeedsConstructor(temporarySucker, mySoil, myplantData.plantPrefabName, timeBeforeSeedReplant);
                 myInventory.SuckersList.Add(mySeed);
                 mySoil.mySoilsData.has_babyplant = true;
             }
         }
     }
 }
예제 #9
0
    public void SoilClicked(Soils soil)
    {
        if (inventory)
        {
            if (inventory.currentItem != null && doNotPlant == false) //changed from if(inventory.currentItem != null) which evaluated to 'false' even when there was an item there
            {
                switch (inventory.currentItem.itemType)
                {
                case InventoryItem.itemTypes.SEED:

                    if (inventory.currentItem.associatedPrefab != null)
                    {
                        if (inventory.currentItem.associatedPrefab.GetComponent <PlantData>() != null && !soil.mySoilsData.is_planted)
                        {
                            soil.mySoilsData.is_planted = true;
                            GameObject.Find("MyScripts").GetComponent <Planting>().PlantingFunction(soil.gameObject, inventory.currentItem.associatedPrefab, 0);
                            inventory.RemoveFromInventory(inventory.currentItem.itemName);
                        }
                    }
                    break;

                case InventoryItem.itemTypes.TOOL:

                    if (inventory.currentItem.itemName == "WateringCan" && soil.mySoilsData.water_lvl < 50)
                    {
                        soil.mySoilsData.water_lvl += amountOfWaterToAdd;

                        inventory.RemoveFromInventory(inventory.currentItem.itemName);

                        return;     //we must return after each of these cases to avoid crashing when the last object is removed from the inventory
                    }

                    if (inventory.currentItem.itemName == "Fertilizer" && soil.mySoilsData.nutrient_lvl < 50)
                    {
                        //Debug.Log("before soil.nutrientInSoil:" + soil.nutrientInSoil+ " amountOfNutrientToAdd:" + amountOfNutrientToAdd);
                        soil.mySoilsData.nutrient_lvl += amountOfNutrientToAdd;
                        //Debug.Log("after nutrient" + soil.nutrientInSoil);
                        inventory.RemoveFromInventory(inventory.currentItem.itemName);
                        return;
                    }
                    if (inventory.currentItem.itemName == "Inspector" && soil.mySoilsData.nutrient_lvl < 50)
                    {
                        if (infoPopup != null && mainCamera != null && soil.readOnlyPlantData != null)     //if we have the infoPopup reference, the main camera reference, and the soil clicked has a plant on it
                        {
                            if (infoPopup.activeInHierarchy == true && inspectingThisSoil == soil)
                            {
                                infoPopup.SetActive(false);
                                return;
                            }

                            if (infoPopup.activeInHierarchy == false)
                            {
                                //ADDED BY GIOVANNI: it calls function in questManager to set the inspector quest as completed
                                questManager.inspectorQuest();
                                //ADDED BY GIOVANNI

                                infoPopup.SetActive(true);
                            }

                            inspectingThisSoil = soil;
                        }
                        return;
                    }
                    break;
                }
            }
        }
    }
예제 #10
0
        /// <summary>
        /// Returns soil water uptake from each zone by the static tree model
        /// </summary>
        /// <param name="soilstate"></param>
        /// <returns></returns>
        public List<Soils.Arbitrator.ZoneWaterAndN> GetSWUptakes(Soils.Arbitrator.SoilState soilstate)
        {
            double SWDemand = 0;  // Tree water demand (L)
            double PotSWSupply = 0; // Total water supply (L)

            foreach (ZoneInfo ZI in ZoneInfoList)
            {
                Soils.SoilWater S = Apsim.Find(ZI.zone, typeof(Soils.SoilWater)) as Soils.SoilWater;
                SWDemand += S.Eo * (1 / (1 - ZI.Shade / 100) - 1) * ZI.zone.Area * 10000;
            }

            List<ZoneWaterAndN> Uptakes = new List<ZoneWaterAndN>();

            foreach (ZoneWaterAndN Z in soilstate.Zones)
            {
                foreach (ZoneInfo ZI in ZoneInfoList)
                {
                    if (Z.Name == ZI.zone.Name)
                    {
                        ZoneWaterAndN Uptake = new ZoneWaterAndN();
                        //Find the soil for this zone
                        Zone ThisZone = new Zone();
                        Soils.Soil ThisSoil = new Soils.Soil();

                        foreach (Zone SearchZ in Apsim.ChildrenRecursively(Parent, typeof(Zone)))
                            if (SearchZ.Name == Z.Name)
                                ThisSoil = Apsim.Find(SearchZ, typeof(Soils.Soil)) as Soils.Soil;

                        Uptake.Name = Z.Name;
                        double[] SW = Z.Water;
                        Uptake.NO3N = new double[SW.Length];
                        Uptake.NH4N = new double[SW.Length];
                        Uptake.Water = new double[SW.Length];
                        for (int i = 0; i <= SW.Length - 1; i++)
                        {
                            double[] LL15mm = MathUtilities.Multiply(ThisSoil.LL15,ThisSoil.Thickness);
                            Uptake.Water[i] = (SW[i] - LL15mm[i]) * ZI.RLD[i];
                            PotSWSupply += Uptake.Water[i] * ZI.zone.Area * 10000;
                        }
                        Uptakes.Add(Uptake);
                    }
                }
            }
            // Now scale back uptakes if supply > demand
            double F = 0;  // Uptake scaling factor
            if (PotSWSupply > 0)
            {
                F = SWDemand / PotSWSupply;
                if (F > 1)
                    F = 1;
            }
            else
                F = 1;

            foreach (ZoneWaterAndN Z in Uptakes)
                Z.Water = MathUtilities.Multiply_Value(Z.Water, F);
            return Uptakes;
        }
예제 #11
0
        /// <summary>
        /// Returns soil Nitrogen uptake from each zone by the static tree model
        /// </summary>
        /// <param name="soilstate"></param>
        /// <returns></returns>
        public List<Soils.Arbitrator.ZoneWaterAndN> GetNUptakes(Soils.Arbitrator.SoilState soilstate)
        {
            List<ZoneWaterAndN> Uptakes = new List<ZoneWaterAndN>();

            foreach (ZoneWaterAndN Z in soilstate.Zones)
            {
                foreach (ZoneInfo ZI in ZoneInfoList)
                {
                    if (Z.Name == ZI.zone.Name)
                    {
                        ZoneWaterAndN Uptake = new ZoneWaterAndN();
                        //Find the soil for this zone
                        Zone ThisZone = new Zone();
                        Soils.Soil ThisSoil = new Soils.Soil();

                        foreach (Zone SearchZ in Apsim.ChildrenRecursively(Parent, typeof(Zone)))
                            if (SearchZ.Name == Z.Name)
                                ThisSoil = Apsim.Find(SearchZ, typeof(Soils.Soil)) as Soils.Soil;

                        Uptake.Name = Z.Name;
                        double[] SW = Z.Water;
                        Uptake.NO3N = new double[SW.Length];
                        Uptake.NH4N = new double[SW.Length];
                        Uptake.Water = new double[SW.Length];
                        //for (int i = 0; i <= SW.Length-1; i++)
                        //    Uptake.NO3N[i] = Z.NO3N[i] * ZI.RLD[i];

                        Uptakes.Add(Uptake);
                    }
                }
            }
            return Uptakes;
        }
예제 #12
0
 private void OnTillageCompleted(object sender, Soils.TillageType tillageType)
 {
     tillageCnCumWater = tillageType.cn_rain;
     tillageCnRed = tillageType.cn_red;
     cumWaterSinceTillage = 0;
 }
예제 #13
0
        public FitnessFunctions(RunItem runItem, bool singleRun, List <OptiParameter> ParamList, List <OptiObservation> ObservList, ObservationItem ObservItem)
            : this()
        {
            if (runItem == null)
            {
                throw new Exception("RunItem is empty: Select a run in the combobox");
            }
            this.RunInfos  = RunDefinitions(runItem, singleRun).ToList();
            multiMultiYear = runItem.MultiMultiYear;
            multiFirstYear = runItem.MultiFirstYear;
            multiLastYear  = runItem.MultiLastYear;

            observItem_ = ObservItem;
            observItem_.updateObservationDictionary();

            observList_ = ObservList;

            foreach (var item in this.RunInfos)
            {
                item.Management = item.Management.Clone();
                item.RunOptions = item.RunOptions.Clone();
                item.Site       = item.Site.Clone();
                item.Soil       = item.Soil.Clone();
                item.Variety    = item.Variety.Clone();
                item.NonVariety = item.NonVariety.Clone();

                this.Managements.Add(item.Management);
                this.RunOptions.Add(item.RunOptions);
                this.Sites.Add(item.Site);
                this.Soils.Add(item.Soil);
                this.Varieties.Add(item.Variety);
                this.NonVarieties.Add(item.NonVariety);
            }

            // Update of ParamSubModel

            foreach (var item in this.Varieties.First().ParamValue.Keys)
            {
                this.ParamSubModel.Add(item, "Variety");
            }

            foreach (var item in this.NonVarieties.First().ParamValue.Keys)
            {
                this.ParamSubModel.Add(item, "NonVariety");
            }


            // Initialisation of ParamList
            List <string> temp = new List <string>();

            foreach (var item in ParamList)
            {
                switch (this.ParamSubModel[item.Name])
                {
                case "Management":
                    temp = Managements.Select(ii => ii.Name).ToList(); optimizeManagementParameter = true;
                    break;

                case "Soil":
                    temp = Soils.Select(ii => ii.Name).ToList(); optimizeSoilParameter = true;
                    break;

                case "NonVariety":
                    temp = NonVarieties.Select(ii => ii.Name).ToList(); optimizeNonVarietalParameter = true;
                    break;

                case "Variety":
                    temp = Varieties.Select(ii => ii.Name).ToList(); optimizeVarietalParameter = true;
                    break;

                default: break;
                }

                while (temp.Count != 0)
                {
                    if (!ParamValueDictio.ContainsKey(temp[0]))
                    {
                        ParamValueDictio.Add(temp[0], new Dictionary <string, double>());
                    }
                    if (!ParamValueDictio[temp[0]].ContainsKey(item.Name))// should always be true
                    {
                        ParamValueDictio[temp[0]].Add(item.Name, 0);
                        paramValueDictioSize++;
                    }

                    string typeToRemove = temp[0];
                    temp.RemoveAll(elem => elem == typeToRemove);
                }
            }
        }
예제 #14
0
        /// <summary>
        /// Returns soil water uptake from each zone by the static tree model
        /// </summary>
        /// <param name="soilstate"></param>
        /// <returns></returns>
        public List<Soils.Arbitrator.ZoneWaterAndN> GetSWUptakes(Soils.Arbitrator.SoilState soilstate)
        {
            double Etz = treeZoneWater.Eo; //Eo of Tree Zone

            SWDemand = 0;
            foreach (Zone ZI in ZoneList)
                SWDemand += Etz*GetShade(ZI) / 100 * ZI.Area * 10000;

            IndividualTreeWaterDemand = SWDemand / NumberOfTrees;

            List<ZoneWaterAndN> Uptakes = new List<ZoneWaterAndN>();
            double PotSWSupply = 0; // Total water supply (L)

            foreach (ZoneWaterAndN Z in soilstate.Zones)
            {
                foreach (Zone ZI in ZoneList)
                {
                    if (Z.Name == ZI.Name)
                    {
                        ZoneWaterAndN Uptake = new ZoneWaterAndN();
                        //Find the soil for this zone
                        Soils.Soil ThisSoil = null;

                        foreach (Zone SearchZ in forestryZones)
                            if (SearchZ.Name == Z.Name)
                            {
                                ThisSoil = Apsim.Find(SearchZ, typeof(Soils.Soil)) as Soils.Soil;
                                break;
                            }

                        Uptake.Name = Z.Name;
                        double[] SW = Z.Water;
                        Uptake.NO3N = new double[SW.Length];
                        Uptake.NH4N = new double[SW.Length];
                        Uptake.Water = new double[SW.Length];
                        double[] LL15mm = MathUtilities.Multiply(ThisSoil.LL15, ThisSoil.Thickness);
                        double[] RLD = GetRLD(ZI);

                        for (int i = 0; i <= SW.Length - 1; i++)
                        {
                            Uptake.Water[i] = Math.Max(SW[i] - LL15mm[i],0.0) * BaseKL*RLD[i];
                            PotSWSupply += Uptake.Water[i] * ZI.Area * 10000;
                        }
                        Uptakes.Add(Uptake);
                        break;
                    }
                }
            }
            // Now scale back uptakes if supply > demand
            double F = 0;  // Uptake scaling factor
            if (PotSWSupply > 0)
            {
                F = SWDemand / PotSWSupply;
                if (F > 1)
                    F = 1;
            }
            else
                F = 1;
            WaterStress = Math.Min(1, Math.Max(0, PotSWSupply / SWDemand));

            List<double> uptakeList = new List<double>();
            foreach (ZoneWaterAndN Z in Uptakes)
            {
                Z.Water = MathUtilities.Multiply_Value(Z.Water, F);
                uptakeList.Add(Z.TotalWater);
            }

            WaterUptake = uptakeList.ToArray();
            return Uptakes;
        }
예제 #15
0
        /// <summary>
        /// Returns soil Nitrogen uptake from each zone by the static tree model
        /// </summary>
        /// <param name="soilstate"></param>
        /// <returns></returns>
        public List<Soils.Arbitrator.ZoneWaterAndN> GetNUptakes(Soils.Arbitrator.SoilState soilstate)
        {
            Zone treeZone = ZoneList[0] as Zone;

            List<ZoneWaterAndN> Uptakes = new List<ZoneWaterAndN>();
            double PotNO3Supply = 0; // Total N supply (kg)

            double NDemandkg = GetNDemandToday()*10 * treeZone.Area * 10000;

            foreach (ZoneWaterAndN Z in soilstate.Zones)
            {
                foreach (Zone ZI in ZoneList)
                {
                    if (Z.Name == ZI.Name)
                    {
                        ZoneWaterAndN Uptake = new ZoneWaterAndN();
                        //Find the soil for this zone
                        Soils.Soil ThisSoil = null;

                        foreach (Zone SearchZ in forestryZones)
                            if (SearchZ.Name == Z.Name)
                            {
                                ThisSoil = Apsim.Find(SearchZ, typeof(Soils.Soil)) as Soils.Soil;
                                break;
                            }

                        Uptake.Name = Z.Name;
                        double[] SW = Z.Water;

                        Uptake.NO3N = new double[SW.Length];
                        Uptake.NH4N = new double[SW.Length];
                        Uptake.Water = new double[SW.Length];
                        double[] LL15mm = MathUtilities.Multiply(ThisSoil.LL15, ThisSoil.Thickness);
                        double[] BD = ThisSoil.BD;
                        double[] RLD = GetRLD(ZI);

                        for (int i = 0; i <= SW.Length - 1; i++)
                        {
                            Uptake.NO3N[i] = PotentialNO3Uptake(ThisSoil.Thickness[i], Z.NO3N[i], Z.Water[i], RLD[i], RootRadius, BD[i], Kd);
                            PotNO3Supply += Uptake.NO3N[i] * ZI.Area * 10000;
                        }
                        Uptakes.Add(Uptake);
                        break;
                    }
                }
            }
            // Now scale back uptakes if supply > demand
            double F = 0;  // Uptake scaling factor
            if (PotNO3Supply > 0)
            {
                F = NDemandkg / PotNO3Supply;
                if (F > 1)
                    F = 1;
            }
            else
                F = 1;
            NStress = Math.Min(1, Math.Max(0, PotNO3Supply / NDemandkg));

            List<double> uptakeList = new List<double>();
            foreach (ZoneWaterAndN Z in Uptakes)
            {
                Z.NO3N = MathUtilities.Multiply_Value(Z.NO3N, F);
                uptakeList.Add(Z.TotalNO3N);
            }

            NUptake = uptakeList.ToArray();
            return Uptakes;
        }