-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBaseLevel.cs
More file actions
357 lines (312 loc) · 11 KB
/
BaseLevel.cs
File metadata and controls
357 lines (312 loc) · 11 KB
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
using System;
using System.Collections.Generic;
using System.Linq;
using static coil.Navigation;
using static coil.Util;
using static coil.Coilutil;
using static coil.SegDescriptor;
namespace coil
{
public class BaseLevel
{
public LevelConfiguration LevelConfiguration { get; set; }
public int Width;
public int Height;
public Random Rnd { get; protected set; }
//null meaning not owned by any segment
//pointing at a segment will include the xest segment covering it.
//public Dictionary<(int, int), Seg> Rows { get; protected set; }
public Seg[] Rows { get; set; }
public Seg GetRowValue((int,int) pos)
{
var index = pos.Item2*Width+pos.Item1;
return Rows[index];
}
public void SetRowValue((int,int) pos, Seg seg)
{
var index = pos.Item2 * Width + pos.Item1;
Rows[index] = seg;
}
public HitManager Hits;
//Path[1] is the first path. So the first path will leave a trail of 1s in rows.
public LinkedList<Seg> Segs { get; protected set; }
//set up empty board with strong border bigger than the input
protected void InitBoard()
{
Rows = new Seg[Height * Width];
for (var yy = 0; yy < Height; yy++)
{
for (var xx = 0; xx < Width; xx++)
{
//Rows[(xx, yy)] = null;
SetRowValue((xx, yy), null);
}
}
}
// only used by initial random walk.
public int GetAvailableSegmentLengthInDirection((int, int) start, Dir dir, int min = 0, int? max = 0)
{
var res = 0;
var candidate = Add(start, dir);
while (GetRowValue(candidate) == null && Hits.GetCount(candidate) == 0 && InBounds(candidate) && (min == 0 || res < min) && (max == 0 || max == null || res < max))
{
res++;
candidate = Add(candidate, dir);
}
return res;
}
//check dependencies looking some direction from start safely overriding spaces.
//take up an equally distributed set of the space you can fill.
//keep trying til failure
public Seg MakeRandomSegFrom((int, int) start, List<Dir> dirs, int min = 0, int? max = 0)
{
//project over available directions and pick one, then create segment and return it.
var validDirs = new List<Tuple<Dir, int>>();
foreach (var dir in dirs)
{
var availableLength = GetAvailableSegmentLengthInDirection(start, dir, min, max);
if (availableLength > 0)
{
validDirs.Add(new Tuple<Dir, int>(dir, availableLength));
}
}
if (!validDirs.Any())
{
return null;
}
//equally distributed
var choice = validDirs[Rnd.Next(validDirs.Count)];
int len = 0;
if (LevelConfiguration.InitialWanderSetup.GoMax)
{
len = choice.Item2;
}
else
{
len = Rnd.Next(choice.Item2 - 1) + 1;
}
var seg = new Seg(start, choice.Item1, len);
return seg;
}
//only used by initial random walk
private void AddSeg(Seg seg)
{
seg.Index = (uint)Segs.Count + 1;
var candidate = seg.Start;
var ii = 0;
//gotta go to the end
while (ii <= seg.Len)
{
SetRowValue(candidate, seg);
candidate = Add(candidate, seg.Dir);
ii++;
}
Hits.Add(candidate, seg);
// WL("Hits after adding seg.");
// ShowSeg(this);
// ShowHit(this);
Segs.AddLast(seg);
}
/// <summary>
/// construct a level for testing.
/// </summary>
public void SimpleBentPath((int, int) start, List<SegDescriptor> segDescriptors)
{
var cur = start;
foreach (var sd in segDescriptors)
{
var seg = new Seg(cur, sd.Dir, sd.Len);
AddSeg(seg);
cur = Add(seg.Start, seg.Dir, seg.Len);
}
}
public void InitialWander(LevelConfiguration lc)
{
(int,int) start;
if (LevelConfiguration.InitialWanderSetup.StartPoint.HasValue)
{
start = LevelConfiguration.InitialWanderSetup.StartPoint.Value;
}
else
{
start = GetRandomPoint();
}
//hack;
var nextDirs = AllDirs;
int? max = null;
if (LevelConfiguration.InitialWanderSetup.MaxLen.HasValue)
{
max = LevelConfiguration.InitialWanderSetup.MaxLen;
}
var segCount = 0;
while (true)
{
var seg = MakeRandomSegFrom(start, nextDirs, max:max);
if (seg == null)
{
break;
}
AddSeg(seg);
segCount++;
start = GetEnd(seg);
switch (seg.Dir)
{
case Dir.Up:
case Dir.Down:
nextDirs = HDirs;
break;
case Dir.Right:
case Dir.Left:
nextDirs = VDirs;
break;
default:
throw new Exception("Bad");
}
if (LevelConfiguration.InitialWanderSetup.StepLimit.HasValue && LevelConfiguration.InitialWanderSetup.StepLimit.Value == segCount) {
break;
}
}
//this is necessary to spread them out.
if (lc.OptimizationSetup.UseSpaceFillingIndexes)
{
RedoAllIndexesSpaceFilled();
}
}
//this will leave some spurious space at the beginning when doing a full redo
public void RedoAllIndexesSpaceFilled()
{
//var st = Stopwatch.StartNew();
var l = new List<LinkedListNode<Seg>>();
var first = Segs.First;
while (first != null)
{
l.Add(first);
first = first.Next;
}
//var t1 = st.Elapsed;
//var st2 = Stopwatch.StartNew();
SpaceFillIndexes(l);
//WL($"Redoallindexes in {t1}, {st2.Elapsed}");
}
public void SpaceFillIndexes(List<LinkedListNode<Seg>> todo)
{
//figure out the range
//pick indexes for each one
//figure out if there is enough room - if not, redo all
var r0 = todo.First().Previous;
var r1 = todo.Last().Next;
uint rangestart = r0?.Value?.Index ?? 0;
uint rangeend = r1?.Value?.Index ?? uint.MaxValue;
var gap = rangeend - rangestart;
if (gap - 1 < todo.Count)
{
RedoAllIndexesSpaceFilled();
return;
//need to reassign everything
}
//plus one to leave space at the end too
uint chunksize = gap / ((uint)todo.Count + 1);
uint current = rangestart + chunksize;
//WL($"Previous to next: {rangestart} => {rangeend}");
foreach (var el in todo)
{
el.Value.Index = current;
//WL($"Assigned {current}");
current += chunksize;
}
}
public (int, int) GetRandomPoint()
{
int x;
int y;
if (Width > 20 && Height > 20)
{
x = Rnd.Next(Width - 7) + 3;
y = Rnd.Next(Height - 7) + 3;
}
else
{
x = Rnd.Next(Width - 2) + 1;
y = Rnd.Next(Height - 2) + 1;
}
return (x, y);
}
public bool InBounds((int, int) candidate)
{
if (candidate.Item1 == 0 || candidate.Item2 == 0 || candidate.Item1 == Width - 1 || candidate.Item2 == Height - 1)
{
return false;
}
return true;
}
public void MaybeSaveDuringTweaking(bool saveTweaks, int saveEvery, Tweak tweak, int tweakct, int tweakfailct)
{
if (saveTweaks)
{
if (tweakct % saveEvery == 0)
{
Console.WriteLine($"Applied tweak: {tweak} {tweakct}");
SaveWithPath(this, $"../../../tweaks/Tweak-{tweakct}.png");
//SaveEmpty(this, $"../../../tweaks/Tweak-{tweakct}-empty.png");
}
}
if (tweakct % 100 == 0)
{
WL($"Tweakct: {tweakct,6} fails: {tweakfailct,6}");
}
}
public int TotalLength()
{
var len = 1;
foreach (var seg in Segs)
{
len += seg.Len;
}
return len;
}
//return the point one advanced from the given seg and point.
public List<(int, int)> Iterate()
{
var res = new List<(int, int)>();
var currentSeg = Segs.First;
var st = 0;
var current = Segs.First.Value.Start;
res.Add(current);
while (true)
{
if (currentSeg == null || currentSeg.Value == null)
{
break;
}
st++;
if (st <= currentSeg.Value.Len)
{
current = Add(currentSeg.Value.Start, currentSeg.Value.Dir, st);
res.Add(current);
}
else
{
st = 0;
currentSeg = currentSeg.Next;
}
}
if (false)
{
(int, int)? last = null;
foreach (var el in res)
{
if (last != null)
{
var d = GridDist(el, last.Value);
if (d != 1)
{
WL("A");
}
}
last = el;
}
}
return res;
}
}
}