forked from notfood/RimWorld-SeedsPlease
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSeedDef.cs
239 lines (209 loc) · 9.22 KB
/
SeedDef.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
using System.Collections.Generic;
using UnityEngine;
using RimWorld;
using Verse;
using System.Xml.Linq;
using System.Linq;
namespace SeedsPlease
{
public class SeedDef : ThingDef
{
public SeedProperties seed;
public new ThingDef plant;
public ThingDef harvest;
public List<ThingDef> sources = new List<ThingDef> ();
static float AssignMarketValueFromHarvest(ThingDef thingDef)
{
var harvestedThingDef = thingDef.plant.harvestedThingDef;
if (harvestedThingDef == null)
{
return 50f;
}
float factor = thingDef.plant.harvestYield / thingDef.plant.growDays + thingDef.plant.growDays / thingDef.plant.harvestYield;
float value = harvestedThingDef.BaseMarketValue * factor * 2.5f;
if (thingDef.plant.blockAdjacentSow)
{
value *= 9f;
}
if (harvestedThingDef == ThingDefOf.WoodLog)
{
value *= 0.2f;
}
else if (harvestedThingDef.IsAddictiveDrug)
{
value *= 1.3f;
}
else if (harvestedThingDef.IsDrug)
{
value *= 1.2f;
}
else if (harvestedThingDef.IsMedicine)
{
value *= 1.1f;
}
value *= Mathf.Lerp(0.8f, 1.6f, thingDef.plant.sowMinSkill / 20f);
return Mathf.Round(value * 100f) / 100f;
}
public override void ResolveReferences ()
{
base.ResolveReferences ();
if (plant == null || plant.blueprintDef != null) {
return;
}
if (plant.plant != null && plant.plant.Sowable) {
plant.blueprintDef = this;
} else {
Log.Warning ("SeedsPlease :: " + plant.defName + " is not a sowable plant.");
plant = null;
return;
}
if (harvest != null) {
plant.plant.harvestedThingDef = harvest;
} else {
harvest = plant.plant.harvestedThingDef;
}
if (BaseMarketValue <= 0 && harvest != null) {
BaseMarketValue = AssignMarketValueFromHarvest(plant);
}
foreach (var p in sources) {
if (p.plant == null) {
Log.Warning ("SeedsPlease :: " + p.defName + " is not a plant.");
continue;
}
p.blueprintDef = this;
}
#if DEBUG
Log.Message ("\t" + plant + " => " + BaseMarketValue);
#endif
}
public static bool AddMissingSeeds() {
bool isAnyMissing = false;
foreach (var thingDef in DefDatabase<ThingDef>.AllDefs.ToList()) {
if (thingDef.plant == null) {
continue;
}
if (thingDef.blueprintDef != null)
{
continue;
}
if (!thingDef.plant.Sowable) {
continue;
}
if (thingDef.plant.harvestedThingDef == null) {
continue;
}
isAnyMissing = true;
AddMissingSeed(thingDef);
}
return isAnyMissing;
}
static void AddMissingSeed(ThingDef thingDef)
{
string name = thingDef.defName;
foreach (string prefix in SeedsPleaseMod.knownPrefixes)
{
name = name.Replace(prefix, "");
}
name = name.CapitalizeFirst();
var template = ResourceBank.ThingDefOf.Seed_Psychoid;
var seed = new SeedDef()
{
defName = "Seed_" + name,
label = name.ToLower() + " seeds",
plant = thingDef,
harvest = thingDef.plant.harvestedThingDef,
BaseMarketValue = AssignMarketValueFromHarvest(thingDef),
stackLimit = template.stackLimit,
seed = new SeedProperties() { harvestFactor = 1f, seedFactor = 1f, baseChance = 0.95f, extraChance = 0.15f },
tradeTags = template.tradeTags,
thingCategories = template.thingCategories,
soundDrop = template.soundDrop,
soundInteract = template.soundInteract,
statBases = template.statBases,
graphicData = template.graphicData,
description = template.description,
thingClass = template.thingClass,
pathCost = template.pathCost,
rotatable = template.rotatable,
drawGUIOverlay = template.drawGUIOverlay,
alwaysHaulable = template.alwaysHaulable,
comps = template.comps,
altitudeLayer = template.altitudeLayer,
selectable = template.selectable,
useHitPoints = template.useHitPoints,
resourceReadoutPriority = template.resourceReadoutPriority,
category = template.category,
uiIcon = template.uiIcon,
uiIconColor = template.uiIconColor,
};
seed.ResolveReferences();
thingDef.blueprintDef = seed;
foreach(var category in seed.thingCategories) {
category.childThingDefs.Add(seed);
}
DefDatabase<ThingDef>.Add(seed);
DefDatabase<SeedDef>.Add(seed);
var stringBuilder = new System.Text.StringBuilder();
stringBuilder.Append("SeedsPlease :: ");
stringBuilder.Append(thingDef.defName);
stringBuilder.Append(" (");
stringBuilder.Append(thingDef.modContentPack.IsCoreMod ? "Patched" : thingDef.modContentPack.Name);
stringBuilder.Append(") is missing a SeedDef. Autogenerated as ");
stringBuilder.AppendLine(seed.defName);
stringBuilder.AppendLine();
var seedXml =
new XElement("SeedsPlease.SeedDef", new XAttribute("ParentName", "SeedBase"),
new XElement("defName", seed.defName),
new XElement("label", seed.label),
new XElement("plant", thingDef.defName));
stringBuilder.AppendLine(seedXml.ToString());
if (thingDef.plant.harvestedThingDef.IsStuff) {
Log.Warning(stringBuilder.ToString());
return;
}
float yieldCount = Mathf.Max(Mathf.Round(thingDef.plant.harvestYield / 3f), 4f);
var ingredient = new IngredientCount();
ingredient.filter.SetAllow(thingDef.plant.harvestedThingDef, true);
ingredient.SetBaseCount(yieldCount);
var recipe = new RecipeDef()
{
defName = "ExtractSeed_" + name,
label = "extract " + name.ToLower() + " seeds",
description = "Extract seeds from " + thingDef.plant.harvestedThingDef.defName.Replace("Raw", ""),
ingredients = new List<IngredientCount>() { ingredient },
defaultIngredientFilter = ingredient.filter,
fixedIngredientFilter = ingredient.filter,
products = new List<ThingDefCountClass>() {
new ThingDefCountClass() { thingDef = seed, count = 3 }
},
researchPrerequisite = thingDef.researchPrerequisites?.FirstOrFallback(),
workAmount = 600f,
workSkill = SkillDefOf.Cooking,
effectWorking = EffecterDefOf.Vomit,
workSpeedStat = StatDefOf.EatingSpeed,
jobString = "Extracting seeds.",
};
DefDatabase<RecipeDef>.Add(recipe);
ResourceBank.ThingDefOf.PlantProcessingTable.recipes.Add(recipe);
var recipeXml =
new XElement("RecipeDef", new XAttribute("ParentName", "ExtractSeed"),
new XElement("defName", recipe.defName),
new XElement("label", recipe.label),
new XElement("description", recipe.description),
new XElement("ingredients",
new XElement("li",
new XElement("filter",
new XElement("thingDefs",
new XElement("li", thingDef.plant.harvestedThingDef.defName))),
new XElement("count", yieldCount))),
new XElement("fixedIngredientFilter",
new XElement("thingDefs",
new XElement("li", thingDef.plant.harvestedThingDef.defName))),
new XElement("products",
new XElement(seed.defName, 3)));
stringBuilder.AppendLine();
stringBuilder.AppendLine(recipeXml.ToString());
Log.Warning(stringBuilder.ToString());
}
}
}