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
|
using AnodyneSharp.Entities;
using AnodyneSharp.Entities.Enemy.Redcave;
using AnodyneSharp.Entities.Gadget.Treasures;
using AnodyneSharp.Registry;
using AnodyneSharp.Sounds;
using Archipelago.MultiClient.Net;
using Archipelago.MultiClient.Net.Enums;
using Archipelago.MultiClient.Net.MessageLog.Messages;
using Archipelago.MultiClient.Net.Models;
using Archipelago.MultiClient.Net.Packets;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace AnodyneArchipelago
{
public class ArchipelagoManager
{
private ArchipelagoSession _session;
private int _itemIndex = 0;
private string _seedName;
private long _endgameCardRequirement = 36;
private readonly Queue<NetworkItem> _itemsToCollect = new();
private readonly Queue<string> _messages = new();
private Task<Dictionary<string, NetworkItem>> _scoutTask;
public long EndgameCardRequirement => _endgameCardRequirement;
public async Task<LoginResult> Connect(string url, string slotName, string password)
{
LoginResult result;
try
{
_session = ArchipelagoSessionFactory.CreateSession(url);
_session.MessageLog.OnMessageReceived += OnMessageReceived;
RoomInfoPacket roomInfoPacket = await _session.ConnectAsync();
_seedName = roomInfoPacket.SeedName;
result = await _session.LoginAsync("Anodyne", slotName, ItemsHandlingFlags.AllItems, null, null, null, password == "" ? null : password);
}
catch (Exception e)
{
result = new LoginFailure(e.GetBaseException().Message);
}
_itemIndex = 0;
_itemsToCollect.Clear();
LoginSuccessful login = result as LoginSuccessful;
if (login.SlotData.ContainsKey("endgame_card_requirement"))
{
_endgameCardRequirement = (long)login.SlotData["endgame_card_requirement"];
}
_scoutTask = Task.Run(() => ScoutAllLocations());
return result;
}
~ArchipelagoManager()
{
Disconnect();
}
public void Disconnect()
{
if (_session == null)
{
return;
}
_session.Socket.DisconnectAsync();
_session = null;
}
private async Task<Dictionary<string, NetworkItem>> ScoutAllLocations()
{
LocationInfoPacket locationInfo = await _session.Locations.ScoutLocationsAsync(_session.Locations.AllLocations.ToArray());
Dictionary<string, NetworkItem> result = new();
foreach (NetworkItem networkItem in locationInfo.Locations)
{
result[_session.Locations.GetLocationNameFromId(networkItem.Location)] = networkItem;
}
return result;
}
public NetworkItem? GetScoutedLocation(string locationName)
{
if (_scoutTask == null || !_scoutTask.IsCompleted || !_scoutTask.Result.ContainsKey(locationName))
{
return null;
}
return _scoutTask.Result[locationName];
}
public string GetItemName(long id)
{
return _session.Items.GetItemName(id);
}
public string GetSeed()
{
return _seedName;
}
public int GetPlayer()
{
return _session.ConnectionInfo.Slot;
}
public void SendLocation(string location)
{
if (_session == null)
{
Plugin.Instance.Log.LogError("Attempted to send location while disconnected");
return;
}
_session.Locations.CompleteLocationChecks(_session.Locations.GetLocationIdFromName("Anodyne", location));
}
public void Update()
{
if (_session == null)
{
// We're not connected.
return;
}
if (_session.Items.AllItemsReceived.Count > _itemIndex)
{
for (int i = _itemIndex; i < _session.Items.AllItemsReceived.Count; i++)
{
string itemKey = $"ArchipelagoItem-{i}";
if (GlobalState.events.GetEvent(itemKey) != 0)
{
continue;
}
GlobalState.events.SetEvent(itemKey, 1);
NetworkItem item = _session.Items.AllItemsReceived[i];
_itemsToCollect.Enqueue(item);
}
_itemIndex = _session.Items.AllItemsReceived.Count;
}
if ((GlobalState.Dialogue == null || GlobalState.Dialogue == "") &&
!GlobalState.ScreenTransition &&
Plugin.Player != null &&
GlobalState.black_overlay.alpha == 0f &&
!Plugin.IsGamePaused)
{
if (_itemsToCollect.Count > 0)
{
NetworkItem item = _itemsToCollect.Dequeue();
HandleItem(item);
}
else if (_messages.Count > 0)
{
GlobalState.Dialogue = _messages.Dequeue();
}
}
}
private static string GetMapNameForDungeon(string dungeon)
{
switch (dungeon)
{
case "Temple of the Seeing One": return "BEDROOM";
case "Apartment": return "APARTMENT";
case "Mountain Cavern": return "CROWD";
case "Hotel": return "HOTEL";
case "Red Grotto": return "REDCAVE";
case "Circus": return "CIRCUS";
default: return "STREET";
}
}
private void HandleItem(NetworkItem item)
{
if (item.Player == _session.ConnectionInfo.Slot && item.Location >= 0)
{
string itemKey = $"ArchipelagoLocation-{item.Location}";
if (GlobalState.events.GetEvent(itemKey) > 0)
{
return;
}
GlobalState.events.SetEvent(itemKey, 1);
}
string itemName = _session.Items.GetItemName(item.Item);
if (itemName.StartsWith("Small Key"))
{
string dungeonName = itemName.Substring(11);
dungeonName = dungeonName.Substring(0, dungeonName.Length - 1);
string mapName = GetMapNameForDungeon(dungeonName);
GlobalState.inventory.AddMapKey(mapName, 1);
}
else if (itemName == "Green Key")
{
GlobalState.inventory.BigKeyStatus[0] = true;
}
else if (itemName == "Blue Key")
{
GlobalState.inventory.BigKeyStatus[2] = true;
}
else if (itemName == "Red Key")
{
GlobalState.inventory.BigKeyStatus[1] = true;
}
else if (itemName == "Jump Shoes")
{
GlobalState.inventory.CanJump = true;
}
else if (itemName == "Health Cicada")
{
GlobalState.MAX_HEALTH += 1;
GlobalState.CUR_HEALTH = GlobalState.MAX_HEALTH;
}
else if (itemName == "Heal")
{
GlobalState.CUR_HEALTH = GlobalState.MAX_HEALTH;
}
else if (itemName == "Swap")
{
GlobalState.inventory.HasTransformer = true;
}
else if (itemName == "Extend")
{
GlobalState.inventory.HasLengthen = true;
}
else if (itemName == "Widen")
{
GlobalState.inventory.HasWiden = true;
}
else if (itemName == "Temple of the Seeing One Statue")
{
// TODO: This and the other two: move while on the same map.
GlobalState.events.SetEvent("StatueMoved_Temple", 1);
}
else if (itemName == "Mountain Cavern Statue")
{
GlobalState.events.SetEvent("StatueMoved_Mountain", 1);
}
else if (itemName == "Red Grotto Statue")
{
GlobalState.events.SetEvent("StatueMoved_Grotto", 1);
}
else if (itemName == "Progressive Red Grotto")
{
GlobalState.events.IncEvent("ProgressiveRedGrotto");
}
else if (itemName == "Card")
{
int cardIndex = GlobalState.inventory.CardCount;
if (cardIndex >= 19)
{
cardIndex += 1;
}
CardTreasure cardTreasure = new(Plugin.Player.Position, cardIndex);
cardTreasure.GetTreasure();
GlobalState.SpawnEntity(cardTreasure);
}
else if (itemName == "Cardboard Box")
{
GlobalState.events.SetEvent("ReceivedCardboardBox", 1);
}
else if (itemName == "Biking Shoes")
{
GlobalState.events.SetEvent("ReceivedBikingShoes", 1);
}
string message;
if (item.Player == _session.ConnectionInfo.Slot)
{
message = $"Found {itemName}!";
}
else
{
string otherPlayer = _session.Players.GetPlayerAlias(item.Player);
message = $"Received {itemName} from {otherPlayer}.";
}
GlobalState.Dialogue = message;
}
public void ActivateGoal()
{
var statusUpdatePacket = new StatusUpdatePacket
{
Status = ArchipelagoClientState.ClientGoal
};
_session.Socket.SendPacket(statusUpdatePacket);
}
private void OnMessageReceived(LogMessage message)
{
switch (message)
{
case ItemSendLogMessage itemSendLogMessage:
if (itemSendLogMessage.IsSenderTheActivePlayer && !itemSendLogMessage.IsReceiverTheActivePlayer)
{
string itemName = _session.Items.GetItemName(itemSendLogMessage.Item.Item);
string messageText;
string otherPlayer = _session.Players.GetPlayerAlias(itemSendLogMessage.Receiver.Slot);
messageText = $"Sent {itemName} to {otherPlayer}.";
SoundManager.PlaySoundEffect("gettreasure");
_messages.Enqueue(messageText);
}
break;
}
}
}
}
|