-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGrid.cs
563 lines (511 loc) · 20.1 KB
/
Grid.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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
using System.Runtime.CompilerServices;
using System.Security;
using System.Security.Cryptography.X509Certificates;
using System.Diagnostics;
using IO.Ably;
using IO.Ably.Realtime;
using System.Runtime.Serialization;
using System.Diagnostics.Contracts;
namespace ReactionDiffusionLibrary;
public class Grid
{
private Species PredSpecies;
private Species PreySpecies;
public static int GridXSize { get; set; }
public static int GridYSize { get; set; }
public List<List<int[]>> Coords { get; set; } = new List<List<int[]>>();
public List<string> Directions { get; set; } = new List<string> { "LEFT", "RIGHT", "UP", "DOWN", "STAY" };
public List<List<List<string>>> AnimalsInGrid { get; set; }
public List<List<List<string>>> UsersInGrid { get; set; }
List<User> KillList = new List<User>(){};
private const int MaxUsers = 10;
private int NumUsers = 0;
private readonly object userCountLock = new object();
public List<User> AllUsers { get; set; } = new List<User>();
IRealtimeChannel channel;
IRealtimeChannel userDataChannel;
private TaskCompletionSource<bool> _presenceReady = new TaskCompletionSource<bool>();
public Grid(int max_x, int max_y)
{
GridXSize = max_x;
GridYSize = max_y;
AnimalsInGrid = new List<List<List<string>>>();
UsersInGrid = new List<List<List<string>>>();
AnimalsInGrid = ClearGrid(AnimalsInGrid);
UsersInGrid = ClearGrid(UsersInGrid);
ClientOptions options = new ClientOptions("_iU5jA.gsQOlQ:j2PoEeg_BXZ5XteImdxyEoHv6f0rOl6J6BaDkEmPD-k") { ClientId = "grid-client" };
// options.LogLevel = LogLevel.Debug;
// options.LogHandler = new CustomLogHandler();
AblyRealtime realtime = new AblyRealtime(options);
channel = realtime.Channels.Get("spatial-ecology-game");
userDataChannel = realtime.Channels.Get("user-data-channel");
}
public void SetSpecies(Species predSpecies, Species preySpecies)
{
PredSpecies = predSpecies;
PreySpecies = preySpecies;
}
public (List<int>, List<int>) AgentsInGridSpace(List<string> agents) {
List<int> preds = new List<int>();
List<int> preys = new List<int>();
foreach (string agentStr in agents)
{
if (agentStr[0] == 'y') preys.Add(int.Parse(agentStr.Substring(1)));
if (agentStr[0] == 'd') preds.Add(int.Parse(agentStr.Substring(1)));
}
return (preys, preds);
}
public List<List<List<string>>> ClearGrid(List<List<List<string>>> GridObj, int MaxX=0, int MaxY=0)
{
if (MaxX ==0 && MaxY==0) {MaxX = GridXSize; MaxY = GridYSize;}
GridObj = new List<List<List<string>>>();
for (int i = 0; i < MaxX; i++)
{
var innerList = new List<List<string>>();
for (int j = 0; j < MaxY; j++)
{
innerList.Add(new List<string>());
}
GridObj.Add(innerList);
}
return GridObj;
}
public void Interact()
{
int ySize = GridYSize;
int xSize = GridXSize;
for (int y_i = 0; y_i < ySize; y_i++)
{
for (int x_i = 0; x_i < xSize; x_i++)
{
List<string> agents = AnimalsInGrid[y_i][x_i];
if (agents.Count == 0) continue;
List<int> preds = new List<int>();
List<int> preys = new List<int>();
(preys, preds) = AgentsInGridSpace(agents);
// Predators breed and feed on prey
Procreate(preds, PredSpecies);
for (int i = 0; i < preds.Count; i++)
{
if (i < preys.Count)
{
Agent predator = PredSpecies.AgentsList[preds[i]];
Agent food = PreySpecies.AgentsList[preys[i]];
predator.Energy += food.Energy;
food.AddToDeathList();
}
}
// Preys procreate
Procreate(preys, PreySpecies);
// Check for special procreation cases
if (PreySpecies.Dying && preys.Count > 0) SingleProcreation(preys[0], PreySpecies);
if (PredSpecies.Dying && preds.Count > 0) SingleProcreation(preds[0], PredSpecies);
}
}
}
public static void Procreate(List<int> speciesList, Species speciesObj)
{
for (int i = 0; i < speciesList.Count - 1; i += 2)
{
Agent mum = speciesObj.AgentsList[speciesList[i]];
Agent dad = speciesObj.AgentsList[speciesList[i + 1]];
mum.Energy -= speciesList.Count;
dad.Energy -= speciesList.Count;
if (mum.Energy + dad.Energy > speciesObj.MinProcreationEnergy) mum.Procreate(dad);
}
}
public static void InitialiseAgents(Species speciesObj)
{
for (int i = 0; i < speciesObj.NumAgents; i++)
{
Agent thisAgent = new Agent(speciesObj, i);
speciesObj.AgentsList.Add(thisAgent);
int agentX = (int)speciesObj.SpeciesCoords[i][0];
int agentY = (int)speciesObj.SpeciesCoords[i][1];
string yOrDCondition = (speciesObj.PredOrPrey == "Prey") ? "y" : "d";
speciesObj.Grid.AnimalsInGrid[agentX][agentY].Add($"{yOrDCondition}{i}");
}
}
public static void SingleProcreation(int agentId, Species speciesObj)
{
Agent mum = speciesObj.AgentsList[agentId];
if (mum.Energy > speciesObj.MinProcreationEnergy) mum.Procreate();
}
public async Task BroadcastGameState()
{
Stopwatch publishStopwatch = new Stopwatch();
string base64payload = VisualizeMap(this.AnimalsInGrid, this.UsersInGrid);
publishStopwatch.Start();
var result = await channel.PublishAsync("image-message", base64payload);
publishStopwatch.Stop();
}
public async Task PresenceMembers()
{
var presenceMembers = await userDataChannel.Presence.GetAsync(true);
Console.WriteLine($"There are {presenceMembers.Count()} members on this channel");
List<Color> AvailableColors = new List<Color> {
new Color(34,56,200),
new Color(100, 170, 200),
new Color(30,100,230),
new Color(150,100,255),
new Color(200, 200, 200),
new Color(143,87,100)
// Some more
};
foreach (var user in presenceMembers)
{
lock (userCountLock)
{
if (NumUsers < AvailableColors.Count) // Assuming you want the first 10 users
{
Console.WriteLine("Member " + user.ClientId + " is already in the channel.");
User thisUser = new User(user.ClientId, AvailableColors[0]);
AllUsers.Add(thisUser);
AvailableColors.RemoveAt(0);
NumUsers++;
}
}
}
if (NumUsers >= 2) _presenceReady.TrySetResult(true);
userDataChannel.Presence.Subscribe(PresenceAction.Enter, member =>
{
lock (userCountLock)
{
if (NumUsers < AvailableColors.Count)
{
Console.WriteLine("Member " + member.ClientId + " entered.");
User thisUser = new User(member.ClientId, AvailableColors[0]);
AllUsers.Add(thisUser);
AvailableColors.RemoveAt(0);
NumUsers++;
}
if (NumUsers >= 2) _presenceReady.TrySetResult(true);
}
});
userDataChannel.Presence.Subscribe(PresenceAction.Leave, member =>
{
lock (userCountLock)
{
Console.WriteLine("Member " + member.ClientId + " left.");
User thisUser = AllUsers.FirstOrDefault(user => user.ID == member.ClientId);
if (thisUser != null)
{
AvailableColors.Add(thisUser.userColour);
AllUsers.RemoveAll(user => user.ID == thisUser.ID);
NumUsers--;
}
else
{
Console.WriteLine("User not found: " + member.ClientId);
}
}
});
// Await two presence members on channel
await _presenceReady.Task;
}
public void DisplayGridState(List<List<List<string>>> grid)
{
for (int x = 0; x < grid.Count; x++)
{
for (int y = 0; y < grid[x].Count; y++)
{
if (grid[x][y].Count > 0)
{
Console.Write($"[{string.Join(",", grid[x][y])}] ");
}
else
{
Console.Write("[ ] ");
}
}
Console.WriteLine();
}
}
public void UpdateUsersInGrid()
{
UsersInGrid = ClearGrid(UsersInGrid);
// Create a copy of AllUsers for safe iteration
var usersCopy = new List<User>(AllUsers);
foreach (User user in usersCopy)
{
var Xs_temp = new List<int>(user.Xs);
var Ys_temp = new List<int>(user.Ys);
foreach (var coord in Xs_temp.Zip(Ys_temp, Tuple.Create))
{
int x = coord.Item1;
int y = coord.Item2;
if (x < GridXSize && y < GridYSize && x >= 0 && y >= 0)
{
UsersInGrid[x][y].Add(user.userColour.ToString());
var numUsers = UsersInGrid[x][y].Count;
var numAgents = AnimalsInGrid[x][y].Count;
if (numUsers > 0 && numAgents > 0) InteractUsersAgents(x, y, user);
if (numUsers > 1) InteractUsersUsers(x, y, user);
}
}
}
// Optionally, update AllUsers with changes if needed
AllUsers = usersCopy;
}
public void InteractUsersAgents(int x, int y, User thisUser)
{
List<string> agents = AnimalsInGrid[x][y];
List<int> preds = new List<int>();
List<int> preys = new List<int>();
(preys, preds) = AgentsInGridSpace(agents);
foreach (int predID in preds){
Agent pred = PredSpecies.AgentsList[predID];
thisUser.Energy -= pred.Energy;
pred.AddToDeathList();
}
foreach (int preyID in preys){
Agent prey = PreySpecies.AgentsList[preyID];
thisUser.Energy += prey.Energy;
prey.AddToDeathList();
}
}
public void InteractUsersUsers(int x, int y, User thisUser)
{
var usersInSpace = UsersInGrid[x][y];
usersInSpace.Remove(thisUser.userColour.ToString());
string otherUserColor = usersInSpace[0];
// Get User otherUser from its ID
User otherUser = AllUsers.FirstOrDefault(user => user.userColour.ToString() == otherUserColor);
if (KillList.Contains(thisUser) || KillList.Contains(otherUser)) return;
if (otherUser.Energy >= thisUser.Energy) {
Console.WriteLine(otherUser.ID + " kills " + thisUser.ID);
this.PreySpecies.InitialiseAgents(thisUser.Coords.Count, thisUser.Xs, thisUser.Ys);
otherUser.Energy += thisUser.Energy;
KillList.Add(thisUser);
} else {
Console.WriteLine(thisUser.ID + " kills " + otherUser.ID);
this.PreySpecies.InitialiseAgents(otherUser.Coords.Count, otherUser.Xs, otherUser.Ys);
thisUser.Energy += otherUser.Energy;
KillList.Add(otherUser);
}
}
public void SubscribeToUserMoves()
{
foreach (User user in AllUsers)
{
userDataChannel.Subscribe($"{user.ID}-moves", (message) =>
{
string? direction = message.Data as string;
// Console.WriteLine($"Published direction {direction} to {user.ID}");
if (direction != null)
{
user.MoveQueue.Add(direction);
}
});
}
}
public void MoveUsers()
{
var usersCopy = new List<User>(AllUsers);
foreach (User user in usersCopy)
{
int size = user.Xs.Count * user.Ys.Count;
UpdateUsersInGrid();
var allMoves = new List<string> (user.MoveQueue);
user.MoveQueue.Clear();
if (allMoves.Count == 0) { PublishUserData(user); continue; }
int maxMoves = Math.Max(1, 300000 / size);
for (int i=0; i < Math.Min(allMoves.Count, maxMoves); i++)
{
user.Move(allMoves[i]);
UpdateUsersInGrid();
}
PublishUserData(user);
}
}
public void PublishUserData(User user)
{
int minX = user.Xs.Min();
List<int> Xs_scaled = user.Xs.Select(x => x - minX).ToList();
int minY = user.Ys.Min();
List<int> Ys_scaled = user.Ys.Select(y => y - minY).ToList();
int maxX = Xs_scaled.Max()+1;
int maxY = Ys_scaled.Max()+1;
List<List<List<string>>> littleGrid = new List<List<List<string>>>();
littleGrid = ClearGrid(littleGrid, maxX, maxY);
foreach (var coord in Xs_scaled.Zip(Ys_scaled, Tuple.Create))
{
int x = coord.Item1;
int y = coord.Item2;
littleGrid[x][y].Add(user.userColour.ToString());
}
string littleBinary64EncodedPayload = VisualizeMap(littleGrid);
var data = new
{
clientId = user.ID,
energy = Math.Round(user.Energy),
characterMacro = littleBinary64EncodedPayload,
numPrey = PreySpecies.AgentsList.Count.ToString(),
numPred = PredSpecies.AgentsList.Count.ToString()
};
userDataChannel.Publish("user-data-update", data);
}
public string VisualizeMap(List<List<List<string>>> map)
{
int width = map.Count;
int height = map[0].Count;
int rowPadding = (4 - (width * 3) % 4) % 4;
int imageSize = (width * 3 + rowPadding) * height;
int fileSize = 54 + imageSize; // 54 bytes for headers
using (MemoryStream ms = new MemoryStream())
{
BinaryWriter writer = new BinaryWriter(ms);
// BMP File Header
writer.Write(new char[] { 'B', 'M' }); // BM
writer.Write(fileSize);
writer.Write((int)0); // Reserved
writer.Write(54); // Offset to start of pixel data
// BMP Info Header
writer.Write(40); // Info header size
writer.Write(width);
writer.Write(height);
writer.Write((short)1); // Planes
writer.Write((short)24); // Bits per pixel
writer.Write((int)0); // Compression (none)
writer.Write(imageSize);
writer.Write(0); // X pixels per meter (not specified)
writer.Write(0); // Y pixels per meter (not specified)
writer.Write(0); // Total colors (default)
writer.Write(0); // Important colors (default)
// Pixel Data
for (int y = height - 1; y >= 0; y--)
{
for (int x = 0; x < width; x++)
{
var agents = map[x][y];
var greenCount = agents.Count(a => a.StartsWith("y"));
var redCount = agents.Count(a => a.StartsWith("d"));
var userCount = agents.Count(a => Char.IsDigit(a[0]));
Color color = DetermineColor(greenCount, redCount);
if (userCount > 0) color = Color.Parse(agents[0]);
writer.Write(color.B);
writer.Write(color.G);
writer.Write(color.R);
}
for (int p = 0; p < rowPadding; p++) writer.Write((byte)0); // Padding
}
writer.Flush();
return Convert.ToBase64String(ms.ToArray());
}
}
public string VisualizeMap(List<List<List<string>>> AnimalGrid, List<List<List<string>>> UserGrid)
{
int width = AnimalGrid.Count;
int height = AnimalGrid[0].Count;
int rowPadding = (4 - (width * 3) % 4) % 4;
int imageSize = (width * 3 + rowPadding) * height;
int fileSize = 54 + imageSize;
using (MemoryStream ms = new MemoryStream(fileSize))
{
BinaryWriter writer = new BinaryWriter(ms);
// BMP File Header
writer.Write(new char[] { 'B', 'M' });
writer.Write(fileSize);
writer.Write(0); // Reserved
writer.Write(54); // Offset to pixel data
// BMP Info Header
writer.Write(40);
writer.Write(width);
writer.Write(height);
writer.Write((short)1);
writer.Write((short)24);
writer.Write(0); // Compression
writer.Write(imageSize);
writer.Write(0); // X pixels per meter
writer.Write(0); // Y pixels per meter
writer.Write(0); // Total colors
writer.Write(0); // Important colors
// Pixel Data
for (int y = height - 1; y >= 0; y--)
{
for (int x = 0; x < width; x++)
{
var agents = AnimalGrid[x][y];
var users = UsersInGrid[x][y];
int greenCount = 0;
int redCount = 0;
foreach (var agent in agents)
{
if (agent.StartsWith("y")) greenCount++;
if (agent.StartsWith("d")) redCount++;
}
Color color = DetermineColor(greenCount, redCount);
if (users.Count > 0) {
color = Color.Parse(users[0]); }
writer.Write(color.B);
writer.Write(color.G);
writer.Write(color.R);
}
for (int p = 0; p < rowPadding; p++) writer.Write((byte)0);
}
writer.Flush();
return Convert.ToBase64String(ms.ToArray());
}
}
private static Color DetermineColor(int greenCount, int redCount)
{
if (greenCount >= redCount && greenCount > 0)
{
return greenCount > 1 ? new Color(0, 128, 0) : new Color(0, 204, 0); // Darker or lighter green
}
else if (redCount > greenCount)
{
return redCount > 1 ? new Color(128, 0, 0) : new Color(204, 0, 0); // Darker or lighter red
}
return new Color(255, 255, 255); // White for no agents
}
public struct Color
{
public byte R, G, B;
public Color(byte r, byte g, byte b) { R = r; G = g; B = b; }
// Method to convert Color to string
public override string ToString()
{
return $"{R},{G},{B}";
}
// Static method to parse a string into a Color object
public static Color Parse(string colorString)
{
var parts = colorString.Split(',');
if (parts.Length != 3) throw new FormatException("Invalid color format");
byte r = byte.Parse(parts[0]);
byte g = byte.Parse(parts[1]);
byte b = byte.Parse(parts[2]);
return new Color(r, g, b);
}
}
public bool KillUsers()
{
foreach (User user in KillList) {
AllUsers.Remove(user);
RemoveUserFromGrid(user);
}
KillList.Clear();
return AllUsers.Count > 1;
}
private void RemoveUserFromGrid(User user)
{
foreach (var x in user.Xs)
{
foreach (var y in user.Ys)
{
if (x < GridXSize && y < GridYSize && x >= 0 && y >= 0)
{
UsersInGrid[x][y].Remove(user.userColour.ToString());
}
}
}
}
}
// class CustomLogHandler : ILoggerSink
// {
// public void LogEvent(LogLevel level, string message)
// {
// Console.WriteLine($"Handler LogLevel : {level}, Data :{message}");
// }
// }