forked from scitor/dvRadioJobControl
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJobHandler.cs
More file actions
186 lines (175 loc) · 10.4 KB
/
Copy pathJobHandler.cs
File metadata and controls
186 lines (175 loc) · 10.4 KB
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
using DV;
using DV.Booklets;
using DV.Booklets.Rendered;
using DV.Common;
using DV.InventorySystem;
using DV.Logic.Job;
using DV.ServicePenalty;
using DV.ThingTypes;
using DV.Utils;
using HarmonyLib;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using UnityEngine;
using static HarmonyLib.AccessTools;
namespace dvRadioJobControl
{
public static class JobHandler
{
private static FieldRef<StationController, List<JobOverview>> SpawnedJobOverviews = AccessTools.FieldRefAccess<StationController, List<JobOverview>>("spawnedJobOverviews");
private static Action<JobOverview>? FoundJobOverviewAction;
//code taken partially from vanilla
public static void HandInJob(Job job)
{
bool completed = false;
JobState newState = SingletonBehaviour<JobsManager>.Instance.TryToCompleteAJob(job);
if (newState == JobState.Completed) completed = true;
DisplayableDebt displayableDebt = (completed ? SingletonBehaviour<JobDebtController>.Instance.LastStagedJobDebt : SingletonBehaviour<JobDebtController>.Instance.GetExistingJobDebtForJob(job));
if (displayableDebt != null && !displayableDebt.IsStaged) displayableDebt.UpdateDebtState();
var jobReport = BookletCreator.CreateJobReport(job, displayableDebt, Vector3.zero, Quaternion.identity, WorldMover.OriginShiftParent);
CommsRadioController.PlayAudioFromRadio(JobValidator_Awake_Patch.ValidateSound, PlayerManager.PlayerCamera.transform);
Main.RadioEntry.StartCoroutine(CreateAndInitializePrintedObject(jobReport.gameObject));
if (completed)
{
JobBooklet b = JobBooklet.allExistingJobBooklets.FirstOrDefault(jb => jb.job.ID == job.ID);
SingletonBehaviour<Inventory>.Instance.DropItemFromHandsOrInventory(b.gameObject);
b.DestroyJobBooklet();
SingletonBehaviour<Inventory>.Instance.AddMoney(job.GetWageForTheJob());
CommsRadioController.PlayAudioFromRadio(MoneyPrinterJobValidator_Awake_Patch.MoneySound, PlayerManager.PlayerCamera.transform);
}
}
private static IEnumerator CreateAndInitializePrintedObject(GameObject printedObject)
{
yield return null;
CommsRadioController.PlayAudioFromRadio(PrinterController_Awake_Patch.PrintingSound, PlayerManager.PlayerCamera.transform);
while ((!(printedObject.GetComponent<RenderedTexturesBase>()?.IsGenerated())) is true) yield return null;
if (SingletonBehaviour<Inventory>.Instance && SingletonBehaviour<Inventory>.Instance.HasFreeSlots()) SingletonBehaviour<Inventory>.Instance.AddItemToInventory(printedObject);
else if (PlayerManager.PlayerCamera != null)
{
printedObject.transform.position = PlayerManager.PlayerCamera.transform.position + PlayerManager.PlayerCamera.transform.forward * 0.75f;
Rigidbody component = printedObject.GetComponent<Rigidbody>();
if (component != null)
{
component.velocity = Vector3.zero;
component.angularVelocity = Vector3.zero;
}
if ((bool)SingletonBehaviour<StorageController>.Instance) SingletonBehaviour<StorageController>.Instance.AddItemToWorldStorage(printedObject);
else UnityEngine.Debug.LogError("StorageController doesn't exist! Can't add item to WorldStorage!");
}
else UnityEngine.Debug.LogError("Unexpected: No free space in inventory and PlayerCamera not found!");
}
public static void TryTakeOrDiscardJob(Job job, bool discard = false)
{
var sc = StationController.GetStationByYardID(job.ID.Split('-')[0]);
if (sc != null && sc.logicStation.availableJobs.Contains(job))
{
FoundJobOverviewAction += TryAcceptJobOverview;
Main.RadioEntry.StartCoroutine(GetJobOverview(job, sc, discard));
}
}
private static IEnumerator GetJobOverview(Job job, StationController sc, bool discard = false)
{
SpawnedJobOverviews ??= AccessTools.FieldRefAccess<StationController, List<JobOverview>>("spawnedJobOverviews");
List<JobOverview>? joList = SpawnedJobOverviews(sc);
if ((joList?.Any(jo => jo.job.ID == job.ID)) is not true)
{
joList = null;
StationJobGenerationRange_IsPlayerInRangeForBookletGeneration_Patch.scToOverride = sc;
yield return new WaitUntil(() =>
{
Stopwatch st = Stopwatch.StartNew();
joList = SpawnedJobOverviews(sc);
return ((joList is not null && joList is { Count: > 0 }) || st.ElapsedMilliseconds > 5000);
});
}
var jobOverview = joList.FirstOrDefault(jo => jo.job.ID == job.ID);
if (discard)
{
if (jobOverview != null)
{
jobOverview.job.ExpireJob();
jobOverview.DestroyJobOverview();
}
}
else
{
FoundJobOverviewAction?.Invoke(jobOverview);
FoundJobOverviewAction -= TryAcceptJobOverview;
}
yield return new WaitForSeconds(2f);
StationJobGenerationRange_IsPlayerInRangeForBookletGeneration_Patch.scToOverride = null;
}
private static void TryAcceptJobOverview(JobOverview jobOverview)
{
if (jobOverview == null)
{
UnityEngine.Debug.LogError("jobOverview is null, this shouldn´t have happened");
CommsRadioController.PlayAudioFromRadio(PrinterController_Awake_Patch.ErrorSound, PlayerManager.PlayerCamera.transform);
return;
}
JobState state = jobOverview.job.State;
if (state != JobState.Available)
{
if (state != JobState.Expired)
{
UnityEngine.Debug.LogError(string.Concat(["Job[", jobOverview.job.ID, "] was already taken, but jobOverview: ", jobOverview.name, " was not destroyed! Destroying jobOverview"]));
CommsRadioController.PlayAudioFromRadio(PrinterController_Awake_Patch.ErrorSound, PlayerManager.PlayerCamera.transform);
jobOverview.DestroyJobOverview();
return;
}
var jobEReport = BookletCreator.CreateJobExpiredReport(jobOverview.job, Vector3.zero, Quaternion.identity, WorldMover.OriginShiftParent);
CommsRadioController.PlayAudioFromRadio(PrinterController_Awake_Patch.ErrorSound, PlayerManager.PlayerCamera.transform);
Main.RadioEntry.StartCoroutine(CreateAndInitializePrintedObject(jobEReport.gameObject));
jobOverview.DestroyJobOverview();
return;
}
else
{
StationController stationController = StationController.allStations.FirstOrDefault<StationController>((StationController st) => st.logicStation.availableJobs.Contains(jobOverview.job));
if (!(stationController != null))
{
UnityEngine.Debug.LogError("Job[" + jobOverview.job.ID + "] is in available state, but is not part of any station! Destroying jobOverview");
jobOverview.DestroyJobOverview();
return;
}
Job job = jobOverview.job;
if (!GameFeatureFlags.IsAllowed(GameFeatureFlags.Flag.UseJobValidator))
{
var jobTWReport = BookletCreator.CreateTutorialWarningReport(Vector3.zero, Quaternion.identity, WorldMover.OriginShiftParent);
CommsRadioController.PlayAudioFromRadio(PrinterController_Awake_Patch.ErrorSound, PlayerManager.PlayerCamera.transform);
Main.RadioEntry.StartCoroutine(CreateAndInitializePrintedObject(jobTWReport.gameObject));
return;
}
if (SingletonBehaviour<JobsManager>.Instance.currentJobs.Count >= SingletonBehaviour<LicenseManager>.Instance.GetNumberOfAllowedConcurrentJobs())
{
var jobMLReport = BookletCreator.CreateMissingLicenseReport(job, false, Vector3.zero, Quaternion.identity, WorldMover.OriginShiftParent);
CommsRadioController.PlayAudioFromRadio(PrinterController_Awake_Patch.ErrorSound, PlayerManager.PlayerCamera.transform);
Main.RadioEntry.StartCoroutine(CreateAndInitializePrintedObject(jobMLReport.gameObject));
return;
}
if (!SingletonBehaviour<LicenseManager>.Instance.IsLicensedForJob(JobLicenseType_v2.ToV2List(job.requiredLicenses)))
{
var jobML2Report = BookletCreator.CreateMissingLicenseReport(job, true, Vector3.zero, Quaternion.identity, WorldMover.OriginShiftParent);
CommsRadioController.PlayAudioFromRadio(PrinterController_Awake_Patch.ErrorSound, PlayerManager.PlayerCamera.transform);
Main.RadioEntry.StartCoroutine(CreateAndInitializePrintedObject(jobML2Report.gameObject));
return;
}
if (!SingletonBehaviour<CareerManagerDebtController>.Instance.IsPlayerAllowedToTakeJob())
{
var jobDWReport = BookletCreator.CreateDebtWarningReport(Vector3.zero, Quaternion.identity, WorldMover.OriginShiftParent);
CommsRadioController.PlayAudioFromRadio(PrinterController_Awake_Patch.ErrorSound, PlayerManager.PlayerCamera.transform);
Main.RadioEntry.StartCoroutine(CreateAndInitializePrintedObject(jobDWReport.gameObject));
return;
}
stationController.TakeJobFromStation(jobOverview);
var jobReport = BookletCreator.CreateJobBooklet(job, Vector3.zero, Quaternion.identity, WorldMover.OriginShiftParent, true);
CommsRadioController.PlayAudioFromRadio(JobValidator_Awake_Patch.ValidateSound, PlayerManager.PlayerCamera.transform);
Main.RadioEntry.StartCoroutine(CreateAndInitializePrintedObject(jobReport.gameObject));
return;
}
}
}
}