forked from notfood/RimWorld-SeedsPlease
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJobDriver_PlantWorkWithSeeds.cs
120 lines (98 loc) · 5.39 KB
/
JobDriver_PlantWorkWithSeeds.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
using System.Collections.Generic;
using UnityEngine;
using RimWorld;
using Verse;
using Verse.AI;
using Verse.Sound;
namespace SeedsPlease
{
public abstract class JobDriver_PlantWorkWithSeeds : JobDriver_PlantHarvest
{
const TargetIndex targetCellIndex = TargetIndex.A;
float workDone;
protected override IEnumerable<Toil> MakeNewToils ()
{
yield return Toils_JobTransforms.MoveCurrentTargetIntoQueue (targetCellIndex);
yield return Toils_Reserve.ReserveQueue (targetCellIndex);
var init = Toils_JobTransforms.ClearDespawnedNullOrForbiddenQueuedTargets (targetCellIndex);
yield return init;
yield return Toils_JobTransforms.SucceedOnNoTargetInQueue (targetCellIndex);
yield return Toils_JobTransforms.ExtractNextTargetFromQueue (targetCellIndex);
var clear = Toils_JobTransforms.ClearDespawnedNullOrForbiddenQueuedTargets (targetCellIndex);
yield return Toils_Goto.GotoThing (targetCellIndex, PathEndMode.Touch).JumpIfDespawnedOrNullOrForbidden (targetCellIndex, clear);
yield return HarvestSeedsToil ();
yield return PlantWorkDoneToil ();
yield return Toils_Jump.JumpIfHaveTargetInQueue (targetCellIndex, init);
}
Toil HarvestSeedsToil ()
{
var toil = new Toil ();
toil.defaultCompleteMode = ToilCompleteMode.Never;
toil.tickAction = delegate {
var actor = toil.actor;
var plant = Plant;
if (actor.skills != null) {
actor.skills.Learn (SkillDefOf.Plants, xpPerTick, true);
}
workDone += actor.GetStatValue (StatDefOf.PlantWorkSpeed, true);
if (workDone >= plant.def.plant.harvestWork) {
if (plant.def.plant.harvestedThingDef != null) {
if (actor.RaceProps.Humanlike && plant.def.plant.harvestFailable && Rand.Value > actor.GetStatValue (StatDefOf.PlantHarvestYield, true)) {
MoteMaker.ThrowText ((actor.DrawPos + plant.DrawPos) / 2f, actor.Map, "TextMote_HarvestFailed".Translate (), 3.65f);
} else {
int plantYield = plant.YieldNow ();
ThingDef harvestedThingDef;
var seedDef = plant.def.blueprintDef as SeedDef;
if (seedDef != null) {
var minGrowth = plant.def.plant.harvestMinGrowth;
float parameter;
if (minGrowth < 0.9f) {
parameter = Mathf.InverseLerp (minGrowth, 0.9f, plant.Growth);
} else if (minGrowth < plant.Growth) {
parameter = 1f;
} else {
parameter = 0f;
}
parameter = Mathf.Min (parameter, 1f);
if (seedDef.seed.seedFactor > 0 && Rand.Value < seedDef.seed.baseChance * parameter) {
int count;
if (Rand.Value < seedDef.seed.extraChance) {
count = 2;
} else {
count = 1;
}
Thing seeds = ThingMaker.MakeThing (seedDef, null);
seeds.stackCount = Mathf.RoundToInt (seedDef.seed.seedFactor * count);
GenPlace.TryPlaceThing (seeds, actor.Position, actor.Map, ThingPlaceMode.Near);
}
plantYield = Mathf.RoundToInt (plantYield * seedDef.seed.harvestFactor);
harvestedThingDef = seedDef.harvest;
} else {
harvestedThingDef = plant.def.plant.harvestedThingDef;
}
if (plantYield > 0) {
var thing = ThingMaker.MakeThing (harvestedThingDef, null);
thing.stackCount = plantYield;
if (actor.Faction != Faction.OfPlayer) {
thing.SetForbidden (true, true);
}
GenPlace.TryPlaceThing (thing, actor.Position, actor.Map, ThingPlaceMode.Near, null);
actor.records.Increment (RecordDefOf.PlantsHarvested);
}
}
}
plant.def.plant.soundHarvestFinish.PlayOneShot (actor);
plant.PlantCollected ();
workDone = 0;
ReadyForNextToil ();
return;
}
};
toil.FailOnDespawnedNullOrForbidden (targetCellIndex);
toil.WithEffect (EffecterDefOf.Harvest, targetCellIndex);
toil.WithProgressBar (targetCellIndex, () => workDone / Plant.def.plant.harvestWork, true, -0.5f);
toil.PlaySustainerOrSound (() => Plant.def.plant.soundHarvesting);
return toil;
}
}
}