/
World.cs
500 lines (457 loc) · 17.4 KB
/
World.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
using System;
using System.Collections.Generic;
using System.Text;
using Hailstone.Interface;
namespace Hailstone
{
/// <summary>
/// An identifier for a zone in a world.
/// </summary>
public struct ZoneIndex
{
public ZoneIndex(int X, int Y)
{
this.X = X;
this.Y = Y;
}
/// <summary>
/// The X coordinate of the zone.
/// </summary>
public int X;
/// <summary>
/// The Y coordinate of the zone.
/// </summary>
public int Y;
public static bool operator ==(ZoneIndex A, ZoneIndex B)
{
return A.X == B.X && A.Y == B.Y;
}
public static bool operator !=(ZoneIndex A, ZoneIndex B)
{
return A.X != B.X || A.Y != B.Y;
}
public override bool Equals(object obj)
{
if (obj is ZoneIndex)
return this == (ZoneIndex)obj;
else
return false;
}
public override int GetHashCode()
{
return this.X * 12289 + this.Y * 196613;
}
}
/// <summary>
/// A visual representation for a subset of a domain, with stones representing entries.
/// </summary>
public class World
{
public World(Domain Domain)
{
this.Domain = Domain;
this._Stones = new Dictionary<Entry, Stone>();
}
/// <summary>
/// The edge-length of a zone in a world.
/// </summary>
public static readonly double ZoneSize = 7.0;
/// <summary>
/// Gets the domain this world is associated with.
/// </summary>
public readonly Domain Domain;
/// <summary>
/// Gets the stone for the given entry, or null if it is not in the world.
/// </summary>
public Stone this[Entry Entry]
{
get
{
Stone stone;
this._Stones.TryGetValue(Entry, out stone);
return stone;
}
}
/// <summary>
/// Tries to get a stone for the given entry. Returns false if none exists in this world.
/// </summary>
public bool TryGetStone(Entry Entry, out Stone Stone)
{
return this._Stones.TryGetValue(Entry, out Stone);
}
/// <summary>
/// Determines whether this world contains a stone for the given entry.
/// </summary>
public bool ContainsEntry(Entry Entry)
{
return this._Stones.ContainsKey(Entry);
}
/// <summary>
/// Determines whether this world contains the given stone.
/// </summary>
public bool ContainsStone(Stone Stone)
{
return this._Stones.ContainsValue(Stone);
}
/// <summary>
/// Gets the amount of stones in this world.
/// </summary>
public int Count
{
get
{
return this._Stones.Count;
}
}
/// <summary>
/// Inserts a stone for the given entry. There must not be any existing stones for the entry.
/// </summary>
public Stone Insert(Entry Entry, Vector Position, Vector Velocity)
{
Stone stone = new Stone(Entry);
stone.Position = Position;
stone.Velocity = Velocity;
this._Introduce(stone, Entry);
return stone;
}
/// <summary>
/// Inserts a stone for the given entry. There must not be any existing stones for the entry.
/// </summary>
public void Insert(Stone Stone)
{
this._Introduce(Stone, Stone.Entry);
}
/// <summary>
/// Introduces a stone to the world.
/// </summary>
private void _Introduce(Stone Stone, Entry Entry)
{
foreach (Entry p in Entry.Previous)
{
Stone prev;
if (this._Stones.TryGetValue(p, out prev))
prev.Next = Stone;
}
Entry n = Entry.Next;
Stone next;
if (n != null && this._Stones.TryGetValue(n, out next))
Stone.Next = next;
this._Stones[Entry] = Stone;
this._Insert(Stone, _GetZone(Stone.Position));
this.MakeLeader(Stone);
}
/// <summary>
/// Updates the set of leader stones in response to a change in the associated domain.
/// </summary>
public void UpdateLeaders()
{
foreach (Stone stone in this._Stones.Values)
this.MakeLeader(stone);
}
/// <summary>
/// Makes the given stone in this world a leader stone, allowing it to admit new stones for
/// adjacent entries in the associated domain.
/// </summary>
public void MakeLeader(Stone Stone)
{
_Leader leader = new _Leader();
if (leader.Reset(this, Stone)) this._Leaders.Add(Stone, leader);
}
/// <summary>
/// Gets a stone at the given location, or null if there's none there.
/// </summary>
public Stone Pick(Vector Point)
{
foreach (Stone stone in this._Stones.Values)
{
if ((Point - stone.Position).Length <= stone.Radius)
return stone;
}
return null;
}
/// <summary>
/// Allows a stone to interact with all other stones in the zone with the given index.
/// </summary>
private void _Interact(Stone Stone, ZoneIndex Index, double Time)
{
List<Stone> zone;
if (this._Zones.TryGetValue(Index, out zone))
{
foreach (Stone other in zone)
{
Stone.Interact(Stone, other, Time);
}
}
}
/// <summary>
/// Updates the state of the world by the given time in seconds.
/// </summary>
public void Update(double Time)
{
foreach (var kvp in this._Zones)
{
ZoneIndex index = kvp.Key;
List<Stone> zone = kvp.Value;
foreach (Stone a in zone)
{
foreach (Stone b in zone)
{
if (a != b && a.GetHashCode() > b.GetHashCode())
{
Stone.Interact(a, b, Time);
}
}
this._Interact(a, new ZoneIndex(index.X + 1, index.Y), Time);
this._Interact(a, new ZoneIndex(index.X + 1, index.Y + 1), Time);
this._Interact(a, new ZoneIndex(index.X, index.Y + 1), Time);
this._Interact(a, new ZoneIndex(index.X - 1, index.Y), Time);
}
}
List<Action> changes = new List<Action>();
double damping = Math.Pow(Settings.Current.StoneDamping, Time);
foreach (var kvp in this._Zones)
{
ZoneIndex index = kvp.Key;
List<Stone> zone = kvp.Value;
foreach (Stone stone in zone)
{
stone.Update(Time, damping);
ZoneIndex nextindex = _GetZone(stone.Position);
if (nextindex != index)
{
List<Stone> z = zone;
Stone s = stone;
ZoneIndex f = index;
ZoneIndex t = nextindex;
changes.Add(delegate
{
this._Remove(s, f, z);
this._Insert(s, t);
});
}
}
}
double pressuredamping = Math.Pow(Settings.Current.StoneIntroductionPressureExpansion, Time);
foreach (var kvp in this._Leaders)
{
Stone s = kvp.Key;
_Leader l = kvp.Value;
Stone i;
if (l.Update(this, s, Time, pressuredamping, out i))
{
if (!l.Reset(this, s)) changes.Add(delegate { this._Leaders.Remove(s); });
if (i != null) changes.Add(delegate { if (!this.ContainsEntry(i.Entry)) this.Insert(i); });
}
else
{
if (l.Timeout <= 0.0) changes.Add(delegate { this._Leaders.Remove(s); });
}
}
foreach (Action change in changes)
{
change();
}
}
/// <summary>
/// Renders this world to the current context.
/// </summary>
public void Render(Rectangle Bounds, double Extent)
{
ZoneIndex bl = _GetZone(Bounds.BottomLeft);
ZoneIndex tr = _GetZone(Bounds.TopRight);
bl.X -= 1; tr.X += 1;
bl.Y -= 1; tr.Y += 1;
int zonecount = (tr.X - bl.X) * (tr.Y - bl.Y);
Render r;
Atlas.Begin(out r);
List<Stone> selected = new List<Stone>();
if (zonecount < this._Zones.Count)
{
for (int x = bl.X; x <= tr.X; x++)
for (int y = bl.Y; y <= tr.Y; y++)
{
List<Stone> zone;
if (this._Zones.TryGetValue(new ZoneIndex(x, y), out zone))
this._DrawZone(r, zone, Extent, selected);
}
}
else
{
foreach (var kvp in this._Zones)
{
ZoneIndex index = kvp.Key;
int x = index.X;
int y = index.Y;
if (x >= bl.X && x <= tr.X && y >= bl.Y && y <= tr.Y)
this._DrawZone(r, kvp.Value, Extent, selected);
}
}
foreach (Stone stone in selected)
Atlas.DrawStone(r, stone, Extent);
Atlas.End(r);
}
/// <summary>
/// Draws all the stones in the given zone.
/// </summary>
private void _DrawZone(Render Render, List<Stone> Zone, double Extent, List<Stone> Selected)
{
foreach (Stone stone in Zone)
{
if (Stone.GetSelectionIndex(stone) != uint.MaxValue)
Selected.Add(stone);
else
Atlas.DrawStone(Render, stone, Extent);
}
}
/// <summary>
/// Gets the zone for the given position.
/// </summary>
private static ZoneIndex _GetZone(Vector Position)
{
return new ZoneIndex((int)Math.Floor(Position.X / ZoneSize), (int)Math.Floor(Position.Y / ZoneSize));
}
/// <summary>
/// Inserts a STONE into a ZONE.
/// </summary>
private void _Insert(Stone Stone, ZoneIndex Index)
{
List<Stone> zone;
if (!this._Zones.TryGetValue(Index, out zone))
this._Zones[Index] = zone = new List<Stone>();
zone.Add(Stone);
}
/// <summary>
/// Removes a stone from a zone.
/// </summary>
private void _Remove(Stone Stone, ZoneIndex Index, List<Stone> Zone)
{
Zone.Remove(Stone);
if (Zone.Count == 0) this._Zones.Remove(Index);
}
/// <summary>
/// Gets the repulsive pressure felt by the given stone.
/// </summary>
private double _Pressure(Stone Stone, out Vector Direction)
{
double pressure = 0.0;
Direction = Vector.Zero;
ZoneIndex centerindex = _GetZone(Stone.Position);
for (int x = centerindex.X - 1; x <= centerindex.X + 1; x++)
for (int y = centerindex.Y - 1; y <= centerindex.Y + 1; y++)
{
List<Stone> zone;
if (this._Zones.TryGetValue(new ZoneIndex(x, y), out zone))
foreach (Stone other in zone)
if (other != Stone)
{
Vector dif = Stone.Position - other.Position;
double len = Math.Max(dif.Length, other.Radius);
double mag = 1.0 / (len * len);
Vector force = dif * (mag / len);
Direction += force;
pressure += mag;
}
}
return pressure;
}
/// <summary>
/// Contains information about a leader stone, a stone that will introduce new entries.
/// </summary>
private class _Leader
{
/// <summary>
/// The maximum pressure for the leader stone before the next stone is introduced. This will
/// decrease over time to insure the new stone will be admitted eventually.
/// </summary>
public double PressureThreshold;
/// <summary>
/// The idle time before this leader is discarded.
/// </summary>
public double Timeout;
/// <summary>
/// The next entry to be introduced by this leader.
/// </summary>
public Entry Next;
/// <summary>
/// Prepares this leader for the next entry. Returns false if there are no entries left to introduce.
/// </summary>
public bool Reset(World World, Stone Stone)
{
this.PressureThreshold = Settings.Current.StoneIntroductionPressureThreshold;
this.Timeout = Settings.Current.StoneIntroductionTimeout;
this.Next = GetNext(World, Stone);
return this.Next != null;
}
/// <summary>
/// Updates this leader state by the given amount of time. Returns true if a new stone was introduced.
/// </summary>
public bool Update(World World, Stone Stone, double Time, double Expansion, out Stone Introduced)
{
Vector dir;
if (World._Pressure(Stone, out dir) < this.PressureThreshold)
{
if (World.ContainsEntry(this.Next))
{
this.Next = GetNext(World, Stone);
if (this.Next == null)
{
Introduced = null;
return true;
}
}
Introduced = new Stone(this.Next);
Introduced.Position = Stone.Position;
double dirlen = dir.Length;
if (dirlen > 0.001)
{
dir = dir.Normal;
Introduced.Velocity = Stone.Velocity + dir * Settings.Current.StoneIntroductionSpeed;
Introduced.Position += dir * Stone.Radius;
}
_Tweak(this.Next, ref Introduced.Position);
return true;
}
else
{
this.PressureThreshold *= Expansion;
this.Timeout -= Time;
Introduced = null;
return false;
}
}
/// <summary>
/// Changes a vector slightly to make it distinct from other vectors.
/// </summary>
private static void _Tweak(object Object, ref Vector Vector)
{
int h = Object.GetHashCode();
int x = h & 0xFF;
int y = (h & 0xFF00) >> 8;
Vector.X += (x - 127.0) * 0.0001;
Vector.Y += (y - 127.0) * 0.0001;
}
/// <summary>
/// Gets the next entry to be introduced by the given stone, or null if there are none left.
/// </summary>
public static Entry GetNext(World World, Stone Stone)
{
Entry entry = Stone.Entry;
Entry n = entry.Next;
if (n != null && !World.ContainsEntry(n))
return n;
uint maxweight = 0;
Entry next = null;
foreach (Entry p in entry.Previous)
if (!World.ContainsEntry(p) && p.Weight > maxweight)
{
maxweight = p.Weight;
next = p;
}
return next;
}
}
private Dictionary<ZoneIndex, List<Stone>> _Zones = new Dictionary<ZoneIndex, List<Stone>>();
private Dictionary<Entry, Stone> _Stones = new Dictionary<Entry, Stone>();
private Dictionary<Stone, _Leader> _Leaders = new Dictionary<Stone, _Leader>();
}
}