-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathInteractive.cs
318 lines (267 loc) · 11.7 KB
/
Interactive.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
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
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using ModAPI;
using ModAPI.Attributes;
using TheForest.Items;
using TheForest.Items.Craft;
using TheForest.Utils;
using UnityEngine;
using Input = ModAPI.Input;
namespace Blueprints
{
internal class Interactive : MonoBehaviour
{
// Members
//protected Texture2D myTexture = null;
public static bool BOpened;
public static float FStamina;
public static float FEnergy;
protected Dictionary<int, Receipe> DicItemIdToReceipe;
protected string SCount = "1";
protected string SInput = "Search for item...";
[ExecuteOnGameStart]
[SuppressMessage("ReSharper", "UnusedMember.Local")]
private static void Init()
{
var go = new GameObject("__Interactive__");
go.AddComponent<Interactive>();
Log.Write("Interactive added to scene");
}
//private void Start()
//{
// //myTexture = ModAPI.Resources.GetTexture("Mods/Blueprints/map.jpg", "Blueprints");
// try
// {
// ModAPI.Log.Write(myTexture);
// myTexture = new Texture2D(700, 700, TextureFormat.RGB24, false, true);
// //myTexture.LoadImage(System.IO.File.ReadAllBytes(@"C:\Program Files (x86)\Steam\steamapps\common\The Forest\Mods\Blueprints\map.jpg"));
//
// ModAPI.Log.Write(myTexture);
// }
// catch (Exception e)
// {
// ModAPI.Log.Write(e.Message);
// }
//}
[SuppressMessage("ReSharper", "UnusedMember.Local")]
private void OnGUI()
{
try
{
if (!BOpened) return;
//TheForest.Utils.LocalPlayer.Inventory.CancelNextChargedAttack = true;
//LocalPlayer.Inventory.Block();
//TheForest.Utils.LocalPlayer.Inventory.StashEquipedWeapon(false);
var cY = 30f;
GUI.skin = Gui.Skin;
var bkpMatrix = GUI.matrix;
//GUI.DrawTexture(new Rect(100, 100, 700, 700), myTexture);
SInput = GUI.TextField(new Rect(110, 110, 1000, 30), SInput);
foreach (var item in ItemDatabase.Items)
{
if (!item._name.ToUpper().Contains(SInput.ToUpper())) continue;
if (SInput.Equals(string.Empty)) continue;
//ModAPI.Log.Write(string.Format("{0} found for item {1}", sInput, item._name));
//if (!item._type.Equals(TheForest.Items.Item.Types.)) { continue; }
var bfound = false;
var sRecipe = GetRecipeString(item._id, ref bfound);
GUI.TextField(new Rect(110f, 120f + cY, 620f, 30f), sRecipe);
SCount = GUI.TextField(new Rect(750f, 120f + cY, 120f, 30f), SCount);
if (GUI.Button(new Rect(880f, 120f + cY, 100f, 30f), "Add"))
{
Log.Write($"Craft {SCount} items");
var count = Convert.ToInt32(SCount);
var dictionary = new Dictionary<int, int>();
for (var i = 0; i < count; i++)
if (CheckInventory(item._id, 1, ref dictionary))
{
RemoveItemsFromInventory(ref dictionary);
// add items to inventory
LocalPlayer.Inventory.AddItem(item._id);
dictionary = new Dictionary<int, int>();
}
else
{
// maximum crafted
break;
}
}
//if (GUI.Button(new Rect(670f, 120f + cY, 100f, 30f), "Add 1"))
//{
// Dictionary<int, int> dictionary = new Dictionary<int, int>();
//
// if (CheckInventory(item._id, 1, ref dictionary))
// {
// RemoveItemsFromInventory(ref dictionary);
//
// // add items to inventory
// LocalPlayer.Inventory.AddItem(item._id);
// LocalPlayer.Inventory.CurrentView.ToString();
//
// }
//}
if (GUI.Button(new Rect(1000f, 120f + cY, 100f, 30f), "Add Max"))
{
var dictionary = new Dictionary<int, int>();
var max = ItemDatabase.ItemById(item._id)._maxAmount;
for (var i = 0; i < max; i++)
if (CheckInventory(item._id, 1, ref dictionary))
{
RemoveItemsFromInventory(ref dictionary);
// add items to inventory
LocalPlayer.Inventory.AddItem(item._id);
dictionary = new Dictionary<int, int>();
}
else
{
// maximum crafted
break;
}
}
cY += 30f;
}
GUI.matrix = bkpMatrix;
}
catch (Exception e)
{
Log.Write(e.Message);
}
}
[SuppressMessage("ReSharper", "UnusedMember.Local")]
private void Start()
{
try
{
DicItemIdToReceipe = new Dictionary<int, Receipe>();
foreach (var recipe in ReceipeDatabase.Receipes)
{
if (DicItemIdToReceipe.ContainsKey(recipe._productItemID))
continue;
DicItemIdToReceipe.Add(recipe._productItemID, recipe);
}
Log.Write("Dictionary initialized.");
}
catch (Exception e)
{
Log.Write(e.Message);
}
}
private string GetRecipeString(int id, ref bool bNoEntry)
{
try
{
if (!DicItemIdToReceipe.ContainsKey(id))
{
bNoEntry = true;
return string.Empty;
}
var sRecipe = DicItemIdToReceipe[id]._name;
if (sRecipe == string.Empty)
sRecipe = ItemDatabase.ItemById(DicItemIdToReceipe[id]._productItemID)._name;
bNoEntry = true;
//foreach (var ingredient in dicItemIdToReceipe[id]._ingredients)
//{
// sRecipe += string.Format(", [Item: {0} - Amount: {1}]", TheForest.Items.ItemDatabase.ItemById(ingredient._itemID)._name, ingredient._amount);
//}
return sRecipe;
}
catch (Exception e)
{
Log.Write(e.Message);
return string.Empty;
}
}
[SuppressMessage("ReSharper", "UnusedMember.Local")]
private void Update()
{
if (!Input.GetButtonDown("Interactive", "Blueprints")) return;
//TheForest.Utils.LocalPlayer.Inventory.CancelNextChargedAttack = true;
//LocalPlayer.Inventory.Block();
if (BOpened)
{
LocalPlayer.FpCharacter.UnLockView();
SInput = "Search for item...";
//LocalPlayer.Inventory.UnBlock();
LocalPlayer.Inventory.EquipPreviousWeaponDelayed();
}
else
{
LocalPlayer.FpCharacter.LockView();
FStamina = LocalPlayer.Stats.Stamina;
FEnergy = LocalPlayer.Stats.Energy;
LocalPlayer.Inventory.StashEquipedWeapon(false);
}
BOpened = !BOpened;
}
private static bool CheckInventory(int itemId, int amount, ref Dictionary<int, int> dicIngredients, bool bMax = false)
{
try
{
var bCanCraft = false;
if (ItemDatabase.ItemById(itemId)._maxAmount.Equals(LocalPlayer.Inventory.AmountOf(itemId)))
{
Log.Write($"Maximum amount of item {ItemDatabase.ItemById(itemId)._name} in Inventory.");
return false;
}
foreach (var recipe in ReceipeDatabase.Receipes)
// Recipe for item
if (recipe._productItemID.Equals(itemId))
{
if (!bMax)
foreach (var ingredient in recipe._ingredients)
{
var nameIngredient = ItemDatabase.ItemById(ingredient._itemID)._name;
var nameItem = ItemDatabase.ItemById(itemId)._name;
// Needed to craft
var nNeeded = ingredient._amount * amount;
// In Inventory
var nInInventory = LocalPlayer.Inventory.AmountOf(ingredient._itemID);
Log.Write(
$"Ingredient: {nameIngredient} for Item: {nameItem}. In Inventory: {nInInventory}, needed to Craft {amount} times: {nNeeded}");
// If enough available we can craft
if (nInInventory >= nNeeded)
{
Log.Write(
$"Added to Dictionary: Item: {ItemDatabase.ItemById(ingredient._itemID)._name}, Amount: {nNeeded}");
dicIngredients.Add(ingredient._itemID, nNeeded);
bCanCraft = true;
}
else
{
return false;
}
}
break;
}
Log.Write($"Can craft {ItemDatabase.ItemById(itemId)._name} ({amount}) times => {bCanCraft}");
return bCanCraft;
}
catch (Exception e)
{
Log.Write(e.Message);
return false;
}
}
private static void RemoveItemsFromInventory(ref Dictionary<int, int> dictionary)
{
try
{
// remove items / ingredients from inventory
foreach (var ingredient in dictionary)
foreach (var item in LocalPlayer.Inventory._possessedItems)
//ModAPI.Log.Write(string.Format("Possessed item: {0}, Item to remove: {1}", TheForest.Items.ItemDatabase.ItemById(item._itemId)._name, TheForest.Items.ItemDatabase.ItemById(ingredient.Key)._name));
if (item._itemId.Equals(ingredient.Key))
{
var success = item.Remove(ingredient.Value);
Log.Write(
$"Removed item: {ItemDatabase.ItemById(ingredient.Key)._name}, amount: {ingredient.Value} successfully = {success}");
}
}
catch (Exception e)
{
Log.Write(e.Message);
}
}
}
}