Plant.cs modding errors

Started by Kilroy232, April 17, 2014, 03:12:13 PM

Previous topic - Next topic

Kilroy232

This is directed to Tynan but I will happily take input from anyone.

I am working on making crystal stalagmite grow in the ground similar to plants but I want to create a new thingclass for them called 'Resource_Grow'. I have implemented a few changes but mostly I just want to get a functional build.

My code is as follows:

// Type: Plant
// Assembly: Assembly-CSharp, Version=0.3.5215.40225, Culture=neutral, PublicKeyToken=null
// MVID: 8B64AFE0-C40C-4282-B7C2-8F91E3F891DC
// Assembly location: C:\Users\Nile\Desktop\RimWorldAlpha3eWin\RimWorld410Win\RimWorld410Win_Data\Managed\Assembly-CSharp.dll

using Sound;
using System.Collections.Generic;
using System.Text;
using UnityEngine;

public class Resource_Grow : Thing
{
    private static readonly Material MatSowing = MaterialPool.MatFrom("Things/Plant/Plant_Sowing");
    protected static readonly SoundDef SoundHarvestReady = SoundDef.Named("HarvestReady");
    public float growthPercent = 0.05f;
    private List<int> posIndexList = new List<int>();
    private Color32[] workingColors = new Color32[4];
    public const float BaseGrowthPercent = 0.05f;
    private const float RotDamagePerTick = 0.005f;
    private const int MinFoodFromFoodYieldingPlants = 2;
    private const float MaxAirPressureForDOT = 0.6f;
    private const float SuffocationMaxDOTPerTick = 0.01f;
    private const float GridPosRandomnessFactor = 0.3f;
    private const float MinGrowthToEat = 0.8f;
    private const int TicksWithoutLightBeforeRot = 50000;
    private PlantReproducer reproducer;
    private int age;
    private int ticksSinceLit;

    public bool HarvestableNow
    {
        get
        {
            if (this.def.plant.Harvestable)
                return (double)this.growthPercent > 0.800000011920929;
            else
                return false;
        }
    }

    public bool EdibleNow
    {
        get
        {
            return (double)this.growthPercent > 0.800000011920929;
        }
    }

    public bool Rotting
    {
        get
        {
            if (this.ticksSinceLit > 50000)
                return true;
            if (this.def.plant.LimitedLifespan)
                return this.age > this.def.plant.lifeSpan;
            else
                return false;
        }
    }

    private string GrowthPercentString
    {
        get
        {
            float num = this.growthPercent * 100f;
            if ((double)num > 100.0)
                num = 100.1f;
            return num.ToString("##0");
        }
    }

    public override string LabelMouseover
    {
        get
        {
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.Append(this.def.label);
            stringBuilder.Append(" (" + this.GrowthPercentString + "% growth");
            if (this.Rotting)
                stringBuilder.Append(", dying");
            stringBuilder.Append(")");
            return stringBuilder.ToString();
        }
    }

    private bool HasEnoughLightToGrow
    {
        get
        {
            return Find.GlowGrid.PsychGlowAt(base.Position) <= this.def.plant.minGlowToGrow;
        }
    }

    public override Material DrawMat
    {
        get
        {
            if (this.LifeStage == PlantLifeStage.Sowing)
                return Resource_Grow.MatSowing;
            else
                return base.DrawMat;
        }
    }

    private float LocalFertility
    {
        get
        {
            return Find.FertilityGrid.FertilityAt(this.Position);
        }
    }

    public PlantLifeStage LifeStage
    {
        get
        {
            if ((double)this.growthPercent < 1.0 / 1000.0)
                return PlantLifeStage.Sowing;
            return (double)this.growthPercent > 0.999000012874603 ? PlantLifeStage.Mature : PlantLifeStage.Growing;
        }
    }

    static Resource_Grow()
    {
    }

    public override void SpawnSetup()
    {
        base.SpawnSetup();
        if (this.reproducer == null && (double)this.def.plant.seedEmitAveragePer20kTicks > 0.0)
            this.reproducer = new PlantReproducer(this);
        if (Find.Map.generating && this.def.plant.LimitedLifespan)
            this.age = Random.Range(0, this.def.plant.lifeSpan + 3000);
        for (int index = 0; index < this.def.plant.maxMeshCount; ++index)
            this.posIndexList.Add(index);
        GenList.Shuffle<int>((IList<int>)this.posIndexList);
    }

    public override void ExposeData()
    {
        base.ExposeData();
        Scribe_Values.LookValue<float>(ref this.growthPercent, "growthPercent", 0.0f, false);
        Scribe_Values.LookValue<int>(ref this.age, "age", 0, false);
        Scribe_Values.LookValue<int>(ref this.ticksSinceLit, "ticksSinceLit", 0, false);
    }

    public float Eaten(float nutritionWanted, Pawn eater)
    {
        this.PlantCollected();
        return this.def.food.nutrition;
    }

    public void PlantCollected()
    {
        if (this.def.plant.destroyOnHarvest)
        {
            this.Destroy();
        }
        else
        {
            this.growthPercent = 0.08f;
            Find.MapDrawer.MapChanged(this.Position, MapChangeType.Things);
        }
    }

    public override void TickRare()
    {
        bool enoughLightToGrow = this.HasEnoughLightToGrow;
        if (!enoughLightToGrow)
            this.ticksSinceLit += 250;
        else
            this.ticksSinceLit = 0;
        if (this.LifeStage == PlantLifeStage.Growing && enoughLightToGrow)
        {
            float num1 = (float)((double)this.LocalFertility * (double)this.def.plant.fertilityFactorGrowthRate + (1.0 - (double)this.def.plant.fertilityFactorGrowthRate));
            float num2 = 1f;
            if ((double)DateHandler.CurDayPercent < 0.200000002980232 || (double)DateHandler.CurDayPercent > 0.800000011920929)
                num2 *= 0.5f;
            this.growthPercent += (float)((double)num1 * (double)num2 * 250.0 * ((double)this.def.plant.growthPer20kTicks / 20000.0));
        }
        this.age += 250;
        if (this.Rotting && this.def.plant.LimitedLifespan)
        {
            int amount = Mathf.CeilToInt(1.25f);
            this.TakeDamage(new DamageInfo(DamageTypeDefOf.Rotting, amount, (Thing)null));
        }
        if (this.destroyed || this.reproducer == null)
            return;
        this.reproducer.PlantReproducerTickRare();
    }

    public int FoodYieldNow()
    {
        if (!this.HarvestableNow)
            return 0;
        if ((double)this.def.plant.maxFoodYield <= 1.0)
            return Mathf.RoundToInt(this.def.plant.maxFoodYield);
        int num = Gen.RandomRoundToInt(this.def.plant.maxFoodYield * ((float)this.health / (float)this.def.maxHealth) * this.growthPercent);
        if (num < 2)
            num = Mathf.Min(2, Gen.RandomRoundToInt(this.def.plant.maxFoodYield));
        return num;
    }

    public override void PrintOnto(SectionLayer layer)
    {
        Vector3 vector3 = Gen.TrueCenter((Thing)this);
        Random.seed = this.Position.GetHashCode();
        float max1 = this.def.plant.maxMeshCount != 1 ? 0.5f : 0.05f;
        int num1 = Mathf.CeilToInt(this.growthPercent * (float)this.def.plant.maxMeshCount);
        if (num1 < 1)
            num1 = 1;
        int num2 = 1;
        int num3 = this.def.plant.maxMeshCount;
        switch (num3)
        {
            case 1:
                num2 = 1;
                break;
            case 4:
                num2 = 2;
                break;
            default:
                if (num3 != 9)
                {
                    if (num3 != 16)
                    {
                        if (num3 == 32)
                        {
                            num2 = 5;
                            break;
                        }
                        else
                        {
                            Log.Error((string)(object)this.def + (object)" must have plant.MaxMeshCount that is a perfect square.");
                            break;
                        }
                    }
                    else
                    {
                        num2 = 4;
                        break;
                    }
                }
                else
                {
                    num2 = 3;
                    break;
                }
        }
        float num4 = 1f / (float)num2;
        Vector3 center1 = Vector3.zero;
        Vector2 size = Vector2.zero;
        int num5 = 0;
        int count = this.posIndexList.Count;
        for (int index = 0; index < count; ++index)
        {
            int num6 = this.posIndexList[index];
            float num7 = this.def.plant.visualSizeRange.LerpThroughRange(this.growthPercent);
            if (this.def.plant.maxMeshCount == 1)
            {
                center1 = vector3 + new Vector3(Random.Range(-max1, max1), 0.0f, Random.Range(-max1, max1));
                float num8 = Mathf.Floor(vector3.z);
                if ((double)center1.z - (double)num7 / 2.0 < (double)num8)
                    center1.z = num8 + num7 / 2f;
            }
            else
            {
                center1 = this.Position.ToVector3();
                center1.y = this.def.altitude;
                center1.x += 0.5f * num4;
                center1.z += 0.5f * num4;
                int num8 = num6 / num2;
                int num9 = num6 % num2;
                center1.x += (float)num8 * num4;
                center1.z += (float)num9 * num4;
                float max2 = num4 * 0.3f;
                center1 += new Vector3(Random.Range(-max2, max2), 0.0f, Random.Range(-max2, max2));
            }
            bool flag = (double)Random.value < 0.5;
            Material mat = GenList.RandomListElement<Material>(this.def.folderDrawMats);
            this.workingColors[1].a = this.workingColors[2].a = (byte)((double)byte.MaxValue * (double)this.def.plant.topWindExposure);
            this.workingColors[0].a = this.workingColors[3].a = (byte)0;
            if (this.def.overdraw)
                num7 += 2f;
            size = new Vector2(num7, num7);
            bool flipUv = flag;
            Printer_Plane.PrintPlane(layer, center1, size, mat, 0.0f, flipUv, (Vector2[])null, this.workingColors);
            ++num5;
            if (num5 >= num1)
                break;
        }
        if (this.def.sunShadowInfo == null)
            return;
        float num10 = (double)size.y >= 1.0 ? 0.81f : 0.6f;
        Vector3 center2 = center1;
        center2.z -= size.y / 2f * num10;
        center2.y -= 0.04f;
        Printer_Shadow.PrintShadow(layer, center2, this.def.sunShadowInfo);
    }

    public override string GetInspectString()
    {
        StringBuilder stringBuilder1 = new StringBuilder();
        stringBuilder1.Append(base.GetInspectString());
        stringBuilder1.AppendLine();
        StringBuilder stringBuilder2 = stringBuilder1;
        string key1 = "PercentGrowth";
        object[] objArray1 = new object[1];
        int index1 = 0;
        string growthPercentString = this.GrowthPercentString;
        objArray1[index1] = (object)growthPercentString;
        string str1 = LanguageDatabase.Translate(key1, objArray1);
        stringBuilder2.AppendLine(str1);
        if (this.LifeStage != PlantLifeStage.Sowing)
        {
            if (this.LifeStage == PlantLifeStage.Growing)
            {
                if (!this.HasEnoughLightToGrow)
                {
                    StringBuilder stringBuilder3 = stringBuilder1;
                    string key2 = "NotGrowingNow";
                    object[] objArray2 = new object[1];
                    int index2 = 0;
                    string str2 = GlowUtility.HumanName(this.def.plant.minGlowToGrow).ToLower();
                    objArray2[index2] = (object)str2;
                    string str3 = LanguageDatabase.Translate(key2, objArray2);
                    stringBuilder3.AppendLine(str3);
                }
                else
                    stringBuilder1.AppendLine(LanguageDatabase.Translate("Growing"));
                int numTicks = (int)((1.0 - (double)this.growthPercent) * (20000.0 / (double)this.def.plant.growthPer20kTicks));
                if (this.def.plant.Harvestable)
                {
                    StringBuilder stringBuilder3 = stringBuilder1;
                    string key2 = "HarvestableIn";
                    object[] objArray2 = new object[1];
                    int index2 = 0;
                    string str2 = GenTime.TicksInDaysString(numTicks);
                    objArray2[index2] = (object)str2;
                    string str3 = LanguageDatabase.Translate(key2, objArray2);
                    stringBuilder3.AppendLine(str3);
                }
                else
                {
                    StringBuilder stringBuilder3 = stringBuilder1;
                    string key2 = "FullyGrownIn";
                    object[] objArray2 = new object[1];
                    int index2 = 0;
                    string str2 = GenTime.TicksInDaysString(numTicks);
                    objArray2[index2] = (object)str2;
                    string str3 = LanguageDatabase.Translate(key2, objArray2);
                    stringBuilder3.AppendLine(str3);
                }
            }
            else if (this.LifeStage == PlantLifeStage.Mature)
            {
                if (this.def.plant.Harvestable)
                    stringBuilder1.AppendLine(LanguageDatabase.Translate("ReadyToHarvest"));
                else
                    stringBuilder1.AppendLine(LanguageDatabase.Translate("Mature"));
            }
        }
        return stringBuilder1.ToString();
    }

    public override void Destroy()
    {
        base.Destroy();
        Find.DesignationManager.RemoveAllDesignationsOn((Thing)this);
    }
}



When I try to compile I am getting two errors originating from the same line.
Error message:

Error   1   The best overloaded method match for 'PlantReproducer.PlantReproducer(Plant)' has some invalid arguments   c:\users\nile\documents\visual studio 2013\Projects\Resource_Grow1\Resource_Grow1\Class1.cs
Error   2   Argument 1: cannot convert from 'Resource_Grow' to 'Plant'   c:\users\nile\documents\visual studio 2013\Projects\Resource_Grow1\Resource_Grow1\Class1.cs

The line of code it refers to is this with the bolded text being the error itself:

public override void SpawnSetup()
    {
        base.SpawnSetup();
        if (this.reproducer == null && (double)this.def.plant.seedEmitAveragePer20kTicks > 0.0)
            this.reproducer = new  PlantReproducer(this);
        if (Find.Map.generating && this.def.plant.LimitedLifespan)
            this.age = Random.Range(0, this.def.plant.lifeSpan + 3000);
        for (int index = 0; index < this.def.plant.maxMeshCount; ++index)
            this.posIndexList.Add(index);
        GenList.Shuffle<int>((IList<int>)this.posIndexList);
    }
   

Lastly I am too lazy to replace my name in the directory paths so ya, my name is really Nile :P

Tynan

Well, your crystals don't reproduce, so they don't need a reproducer. You can delete the whole line and the one above it.

The error just means that you can't use the PlantReproducer class with your class because the PlantReproducer can only work with a Plant and your class isn't a Plant.
Tynan Sylvester - @TynanSylvester - Tynan's Blog

Kilroy232

Quote from: Tynan on April 17, 2014, 11:01:56 PM
Well, your crystals don't reproduce, so they don't need a reproducer. You can delete the whole line and the one above it.

The error just means that you can't use the PlantReproducer class with your class because the PlantReproducer can only work with a Plant and your class isn't a Plant.

That not only solves my problem but saves me the trouble of removing that feature. Thank you very much for your insight :D

WorldOfIllusion

as a further question to Tynan, how would you fix this if you wanted your plants to reproduce? Will we need to re-implement the reproducer (and possibly other map-gen stuff)?
Artistically challenged modder seeking artistically talented texturer's help. Please, please, PM me :)