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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
|
using System;
using System.Collections.Generic;
using UnityEngine;
public class Fermenter : MonoBehaviour, Hoverable, Interactable
{
[Serializable]
public class ItemConversion
{
public ItemDrop m_from;
public ItemDrop m_to;
public int m_producedItems = 4;
}
private enum Status
{
Empty,
Fermenting,
Exposed,
Ready
}
private const float updateDT = 2f;
public string m_name = "Fermentation barrel";
public float m_fermentationDuration = 2400f;
public GameObject m_fermentingObject;
public GameObject m_readyObject;
public GameObject m_topObject;
public EffectList m_addedEffects = new EffectList();
public EffectList m_tapEffects = new EffectList();
public EffectList m_spawnEffects = new EffectList();
public Switch m_addSwitch;
public Switch m_tapSwitch;
public float m_tapDelay = 1.5f;
public Transform m_outputPoint;
public Transform m_roofCheckPoint;
public List<ItemConversion> m_conversion = new List<ItemConversion>();
private ZNetView m_nview;
private float m_updateCoverTimer;
private bool m_exposed;
private string m_delayedTapItem = "";
private void Awake()
{
m_nview = GetComponent<ZNetView>();
m_fermentingObject.SetActive(value: false);
m_readyObject.SetActive(value: false);
m_topObject.SetActive(value: true);
if (!(m_nview == null) && m_nview.GetZDO() != null)
{
m_nview.Register<string>("AddItem", RPC_AddItem);
m_nview.Register("Tap", RPC_Tap);
InvokeRepeating("UpdateVis", 2f, 2f);
}
}
public string GetHoverName()
{
return m_name;
}
public string GetHoverText()
{
if (!PrivateArea.CheckAccess(base.transform.position, 0f, flash: false))
{
return Localization.instance.Localize(m_name + "\n$piece_noaccess");
}
switch (GetStatus())
{
case Status.Ready:
{
string contentName2 = GetContentName();
return Localization.instance.Localize(m_name + " ( " + contentName2 + ", $piece_fermenter_ready )\n[<color=yellow><b>$KEY_Use</b></color>] $piece_fermenter_tap");
}
case Status.Fermenting:
{
string contentName = GetContentName();
if (m_exposed)
{
return Localization.instance.Localize(m_name + " ( " + contentName + ", $piece_fermenter_exposed )");
}
return Localization.instance.Localize(m_name + " ( " + contentName + ", $piece_fermenter_fermenting )");
}
case Status.Empty:
return Localization.instance.Localize(m_name + " ( $piece_container_empty )\n[<color=yellow><b>$KEY_Use</b></color>] $piece_fermenter_add");
default:
return m_name;
}
}
public bool Interact(Humanoid user, bool hold)
{
if (hold)
{
return false;
}
if (!PrivateArea.CheckAccess(base.transform.position))
{
return true;
}
switch (GetStatus())
{
case Status.Empty:
{
ItemDrop.ItemData itemData = FindCookableItem(user.GetInventory());
if (itemData == null)
{
user.Message(MessageHud.MessageType.Center, "$msg_noprocessableitems");
return true;
}
AddItem(user, itemData);
break;
}
case Status.Ready:
m_nview.InvokeRPC("Tap");
break;
}
return true;
}
public bool UseItem(Humanoid user, ItemDrop.ItemData item)
{
if (!PrivateArea.CheckAccess(base.transform.position))
{
return false;
}
return AddItem(user, item);
}
private void UpdateVis()
{
UpdateCover(2f);
switch (GetStatus())
{
case Status.Empty:
m_fermentingObject.SetActive(value: false);
m_readyObject.SetActive(value: false);
m_topObject.SetActive(value: false);
break;
case Status.Fermenting:
m_readyObject.SetActive(value: false);
m_topObject.SetActive(value: true);
m_fermentingObject.SetActive(!m_exposed);
break;
case Status.Ready:
m_fermentingObject.SetActive(value: false);
m_readyObject.SetActive(value: true);
m_topObject.SetActive(value: true);
break;
case Status.Exposed:
break;
}
}
private Status GetStatus()
{
if (string.IsNullOrEmpty(GetContent()))
{
return Status.Empty;
}
if (GetFermentationTime() > (double)m_fermentationDuration)
{
return Status.Ready;
}
return Status.Fermenting;
}
private bool AddItem(Humanoid user, ItemDrop.ItemData item)
{
if (GetStatus() != 0)
{
return false;
}
if (!IsItemAllowed(item))
{
return false;
}
if (!user.GetInventory().RemoveOneItem(item))
{
return false;
}
m_nview.InvokeRPC("AddItem", item.m_dropPrefab.name);
return true;
}
private void RPC_AddItem(long sender, string name)
{
if (m_nview.IsOwner() && GetStatus() == Status.Empty)
{
if (!IsItemAllowed(name))
{
ZLog.DevLog("Item not allowed");
return;
}
m_addedEffects.Create(base.transform.position, base.transform.rotation);
m_nview.GetZDO().Set("Content", name);
m_nview.GetZDO().Set("StartTime", ZNet.instance.GetTime().Ticks);
}
}
private void RPC_Tap(long sender)
{
if (m_nview.IsOwner() && GetStatus() == Status.Ready)
{
m_delayedTapItem = GetContent();
Invoke("DelayedTap", m_tapDelay);
m_tapEffects.Create(base.transform.position, base.transform.rotation);
m_nview.GetZDO().Set("Content", "");
m_nview.GetZDO().Set("StartTime", 0);
}
}
private void DelayedTap()
{
m_spawnEffects.Create(m_outputPoint.transform.position, Quaternion.identity);
ItemConversion itemConversion = GetItemConversion(m_delayedTapItem);
if (itemConversion != null)
{
float num = 0.3f;
for (int i = 0; i < itemConversion.m_producedItems; i++)
{
Vector3 position = m_outputPoint.position + Vector3.up * num;
UnityEngine.Object.Instantiate(itemConversion.m_to, position, Quaternion.identity);
}
}
}
private void ResetFermentationTimer()
{
if (GetStatus() == Status.Fermenting)
{
m_nview.GetZDO().Set("StartTime", ZNet.instance.GetTime().Ticks);
}
}
private double GetFermentationTime()
{
DateTime dateTime = new DateTime(m_nview.GetZDO().GetLong("StartTime", 0L));
if (dateTime.Ticks == 0L)
{
return -1.0;
}
return (ZNet.instance.GetTime() - dateTime).TotalSeconds;
}
private string GetContentName()
{
string content = GetContent();
if (string.IsNullOrEmpty(content))
{
return "";
}
ItemConversion itemConversion = GetItemConversion(content);
if (itemConversion == null)
{
return "Invalid";
}
return itemConversion.m_from.m_itemData.m_shared.m_name;
}
private string GetContent()
{
return m_nview.GetZDO().GetString("Content");
}
private void UpdateCover(float dt)
{
m_updateCoverTimer += dt;
if (m_updateCoverTimer > 10f)
{
m_updateCoverTimer = 0f;
Cover.GetCoverForPoint(m_roofCheckPoint.position, out var coverPercentage, out var underRoof);
m_exposed = !underRoof || coverPercentage < 0.7f;
if (m_exposed && m_nview.IsOwner())
{
ResetFermentationTimer();
}
}
}
private bool IsItemAllowed(ItemDrop.ItemData item)
{
return IsItemAllowed(item.m_dropPrefab.name);
}
private bool IsItemAllowed(string itemName)
{
foreach (ItemConversion item in m_conversion)
{
if (item.m_from.gameObject.name == itemName)
{
return true;
}
}
return false;
}
private ItemDrop.ItemData FindCookableItem(Inventory inventory)
{
foreach (ItemConversion item2 in m_conversion)
{
ItemDrop.ItemData item = inventory.GetItem(item2.m_from.m_itemData.m_shared.m_name);
if (item != null)
{
return item;
}
}
return null;
}
private ItemConversion GetItemConversion(string itemName)
{
foreach (ItemConversion item in m_conversion)
{
if (item.m_from.gameObject.name == itemName)
{
return item;
}
}
return null;
}
}
|