/
CircuitRandomizer.cs
520 lines (437 loc) · 19.6 KB
/
CircuitRandomizer.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
using System;
using System.Linq;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Perception.Randomization.Parameters;
using UnityEngine.Perception.Randomization.Randomizers;
using UnityEngine.Perception.Randomization.Randomizers.Utilities;
using UnityEngine.Perception.Randomization.Samplers;
/// <summary>
/// Creates a 2D layer of Resistor and Wire GameObjects on a Breadbord GameObject to form a random connected circuit
/// </summary>
[Serializable]
[AddRandomizerMenu("Perception/Circuit Randomizer")]
public class CircuitRandomizer : Randomizer
{
System.Random random = new System.Random(); // to generate random numbers
[Tooltip("The breadboard to be placed by this Randomizer.")]
public GameObjectParameter breadboard;
[Tooltip("The edge wires starting or terminating in the power rail")]
public GameObjectParameter edgeWires;
[Tooltip("The middle wires starting and terminating in the middle section of breadboard")]
public GameObjectParameter middleWires;
//GameObjects to be displayed
GameObject breadboardContainer;
GameObject edgeContainer;
GameObject middleContainer;
//used to transform and rotate GameObjects
GameObjectOneWayCache breadboardCache;
GameObjectOneWayCache edgeCache;
GameObjectOneWayCache middleCache;
float verticalHoleDistance = 0.635f; // y distance between each breadboard hole (3.532 - -3.461) / 11
float horizontalHoleDistance = 0.635f; // x distance between each breadboard hole (19.685 - -19.694) / 62
float outsideBottomRailY = -5.74f; // outside rail on the bottom y value
float outsideTopRailY = 5.70f; // outside rail on the bottom y value
float wireZ = -0.26f; //z value for all wires and resistors
// lower left reference for placing middle wires
float middleBottomLeftX = -19.67f;
float middleBottomLeftY = -3.49f;
//List to hold wires that fit from the inside power rail
List<GameObject> insideRailList = new List<GameObject>
{};
//List to hold wires that fit from the outside power rail
List<GameObject> outsideRailList = new List<GameObject>
{};
Dictionary<string, int> outsideRailLengthDict =
new Dictionary<string, int>(){
{"Edge10mm", 1},
{"Edge12mm", 2},
{"Edge15mm", 3},
{"Edge17mm", 4}, };
Dictionary<string, int> middleWireLengthDict =
new Dictionary<string, int>(){
{"2mm63", 2},
{"5mm63", 3},
{"7mm63", 4},
{"resistor63v4", 5},
{"12mm63", 6},
{"15mm63", 7},
{"17mm63", 8},
{"20mm63", 9},
{"22mm63", 10},
};
// give higher chance medium length wires
int[] lengthWeights = new int[35] {
2, 2, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10,
};
/// <summary>
/// Method <c>isAlignedWithRail()</c>
/// Checks to see if a wire is in the right x position to be aligned with the power rail.
/// </summary>
protected bool isAlignedWithRail(int xPos){
//7, 13, 19, 25
return (xPos - 1) % 6 != 0 && xPos > 1 && xPos < 61;
}
/// <summary>
/// Method <c>isInsideBoundary()</c>
/// Checks to see if a wire will stay on breadboard or cross the middle gap correctly.
/// </summary>
protected bool isInsideBoundary(int xPos, int yPos, int length, int direction){
if (direction == 0) { // South
return false;
}
else if(direction == 1){ // East
if(xPos + length > 62){
return false;
}
else{
return true;
}
}
else if(direction == 2){ //North
if(yPos >= 0 && yPos <= 4){
int newYPos = yPos - 2 + length - 1 ; //there is a two space gap in the middle of the breadboard and the length is the number of holes the wire spans - 1 since a wire length of n starting at position x would terminate at position x + n - 1
bool reachesNextSection = (newYPos >=5 && newYPos <= 8); // needs to have at least 1 leftover space for another wire
if(reachesNextSection){
return true;
}
else{
return false;
}
}
else{
return false;
}
}
else if(direction == 3){ //West
if(xPos - length < 0){
return false;
}
else{
return true;
}
}
else{
throw new Exception("Invalid direction was chosen");
}
}
/// <summary>
/// Method <c>visitHoles()</c>
/// update visited array with the holes that the wire will span
/// </summary>
/// <returns> int[] ending x and y. if it overlapped, returns the original x and y </returns>
protected static int[] visitHoles(int[,] visited, int startingX, int startingY, int wireLength, int direction){
int endingX = startingX;
int endingY = startingY;
int[,] attemptVisit = new int[63, 10];
Array.Copy(visited, attemptVisit, 630);
bool wireOverlaps = false;
if(direction == 0) { // South
if(startingY - wireLength <= 4){ //offset for the center line of breadboard where there are no holes
wireLength = wireLength - 2;
}
for(int i = 0; i < wireLength; i++){
if(attemptVisit[startingX, startingY - i] == 1){
wireOverlaps = true;
}
attemptVisit[startingX, startingY - i] = 1;
}
endingY = startingY - wireLength - 1;
}
else if(direction == 1){ //East
for(int i = 0; i < wireLength; i++){
if(attemptVisit[startingX + i, startingY] == 1){
wireOverlaps = true;
}
attemptVisit[startingX + i, startingY] = 1;
}
endingX = startingX + wireLength - 1;
}
else if(direction == 2){ //North
if(startingY + wireLength >= 7){ //offset for the center line of breadboard where there are no holes
wireLength = wireLength - 2;
}
for(int i = 0; i < wireLength; i++){
if(attemptVisit[startingX, startingY + i] == 1){
wireOverlaps = true;
}
attemptVisit[startingX, startingY + i] = 1;
}
endingY = startingY + wireLength - 1;
}
else if(direction == 3){ //West
for(int i = 0; i < wireLength; i++){
if(attemptVisit[startingX - i, startingY] == 1){
wireOverlaps = true;
}
attemptVisit[startingX - i, startingY] = 1;
}
endingX = startingX - wireLength + 1;
}
else{
throw new Exception("Invalid direction was chosen");
}
int[] endingPlacement = new int[2];
if(!wireOverlaps){
// Debug.Log("WIRE DOESN'T OVERLAP");
Array.Copy(attemptVisit, visited , 630);
endingPlacement[0] = endingX;
endingPlacement[1] = endingY;
}
else{
// Debug.Log("WIRE OVERLAPS");
endingPlacement[0] = startingX;
endingPlacement[1] = startingY;
}
return endingPlacement;
}
/// <summary>
/// Method <c>placeTopEdgeWire()</c>
/// instantiate a wire prefab and place it at the top edge of the breadboard into the top rail
/// </summary>
/// <returns> void </returns>
protected void placeTopEdgeWire(int x, int wireLength){
bool found = false;
for(int i = 0; i < outsideRailList.Count; i++){
if(outsideRailLengthDict[outsideRailList[i].name] == wireLength){
var bottomEdgeWire = edgeCache.GetOrInstantiate(outsideRailList[i]);
bottomEdgeWire.transform.position = new Vector3(middleBottomLeftX + ((x)*verticalHoleDistance), outsideTopRailY , wireZ);
bottomEdgeWire.transform.rotation = Quaternion.Euler(0, 0, 180);
found = true;
}
}
if(!found){
throw new Exception("Edge wire with length " + wireLength.ToString() + " was not found");
}
}
/// <summary>
/// Method <c>placeBottomEdgeWire()</c>
/// instantiate a wire prefab and place it at the bottom edge of the breadboard into the bottom rail
/// </summary>
/// <returns> void </returns>
protected void placeBottomEdgeWire(int x, int wireLength){
bool found = false;
for(int i = 0; i < outsideRailList.Count; i++){
if(outsideRailLengthDict[outsideRailList[i].name] == wireLength){
var bottomEdgeWire = edgeCache.GetOrInstantiate(outsideRailList[i]);
bottomEdgeWire.transform.position = new Vector3(middleBottomLeftX + ((x)*verticalHoleDistance), outsideBottomRailY , wireZ);
bottomEdgeWire.transform.rotation = Quaternion.Euler(0, 0, 0);
found = true;
}
}
if(!found){
throw new Exception("Edge wire with length " + wireLength.ToString() + " was not found");
}
}
/// <summary>
/// Method <c>placeCenterWire()</c>
/// instantiate a wire prefab and place it in the center of the breadboard
/// </summary>
/// <returns> void </returns>
protected void placeCenterWire(int x, int y, int wireLength, int direction){
bool found = false;
// Add the wires to the lists representing the wires allowed for certain positions
for (var i = 0; i < middleWires.GetCategoryCount(); i++){
GameObject wire = middleWires.GetCategory(i);
if(middleWireLengthDict[wire.name] == wireLength){
var middleWire = middleCache.GetOrInstantiate(wire);
if(y > 4){ //offset for the center line of breadboard where there are no holes
y = y + 2;
}
middleWire.transform.position = new Vector3(middleBottomLeftX + (horizontalHoleDistance*x), middleBottomLeftY + (verticalHoleDistance*y), wireZ);
middleWire.transform.rotation = Quaternion.Euler(0, 0, 90*direction); // ensure rotated properly
found = true;
}
}
if(!found){
throw new Exception("Middle wire with length " + wireLength.ToString() + " was not found");
}
}
/// <summary>
/// Method <c>placeCircuit()</c>
/// places circuit components based on array created by findCircuit
/// </summary>
/// <returns> void </returns>
protected void placeCircuit(List<int[]> placements){
//place first edge wire
int[] edgeWire = placements[0];
placeBottomEdgeWire(edgeWire[0], edgeWire[2]);
//place middle wires
for(int i = 1; i < placements.Count - 1; i++){
placeCenterWire(placements[i][0], placements[i][1], placements[i][2], placements[i][3]);
}
//place top wire
int index = placements.Count - 1;
placeTopEdgeWire(placements[index][0],placements[index][2]);
}
/// <summary>
/// Method <c>findCircuit()</c>
/// generates the wire placements for a connected circuit
/// </summary>
/// <returns> int[,] an array of arrays in the format [x, y, length, direction] </returns>
protected List<int[]> findCircuit(){
Debug.Log("Finding Circuit");
int[,] visited = new int[63, 10];
int[,] cleared = new int[63, 10];
// int[,] circuit = new int[1,4];
List<int[]> circuit = new List<int[]>();
// random edge wire.
int holeNumber = random.Next(50);
int edgeX = ((holeNumber/5)+holeNumber) + 2;
int edgeY = 0;
int edgeDirection = 2; //North
int edgeWireLength = random.Next(4) + 1; //fill up 1-4 of holes
int[] edgeWire = new int[4] {edgeX, edgeY, edgeWireLength, edgeDirection};
int attempts = 0;
bool foundPath = false;
while(!foundPath){
// initialize
circuit.Clear();
circuit.Add(edgeWire);
Array.Copy(cleared, visited, 630);
visitHoles(visited, edgeX, edgeY, edgeWireLength, edgeDirection);
//begin search
int x = edgeX;
int y = 4 - random.Next(5 - edgeWireLength);
int wireLength = lengthWeights[random.Next(35)];
int direction = 1 + 2 * random.Next(2);
int[] placement = new int[2];
placement[0] = x;
placement[1] = y;
for(int i = 0; i < 14; i++){
// get length and direction thats inside boundary
bool overlaps = true;
attempts = 0;
Debug.Log("Ending Placement: " + placement[0].ToString() + ", " + placement[1].ToString());
//final wire
if(placement[1] > 4 && direction == 2 && isAlignedWithRail(placement[0])){ // wire going north in top half should end up in top rail
int randLength = 1 + random.Next(8 - placement[1]);
int originalX = placement[0];
int[] finalWire = new int[4] {originalX, 9, randLength, 2};
placement = visitHoles(visited, originalX, 9, randLength, 0);
if(!(placement[0] == originalX && placement[1] == 9)){ //new placement was found, place the wire
overlaps = false;
circuit.Add(finalWire);
Debug.Log("Placing final wire");
foundPath = true;
break;
}
else{
overlaps = true;
}
}
while(overlaps && attempts < 5){
Debug.Log("Attempt " + attempts.ToString());
while(!isInsideBoundary(x, y, wireLength, direction)){
direction = 1 + random.Next(3);
wireLength = lengthWeights[random.Next(35)];
x = placement[0];
if(placement[1] < 5){
while(y == placement[1]){
y = random.Next(5);
}
}
else{
while(y == placement[1]){
y = 5 + random.Next(4);
}
}
}
Debug.Log("Attempting x: " + x.ToString() + ", y: " + y.ToString() + ", length: " + wireLength.ToString() + ", direction: " + direction.ToString() + "Termination: " +placement[0].ToString() + ", " + placement[1].ToString());
// if it overlaps try again
// check to see if placement overlaps
placement = visitHoles(visited, x, y, wireLength, direction);
attempts = attempts + 1;
if(!(placement[0] == x && placement[1] == y)){ //new placement was found, place the wire
overlaps = false;
}
else{
direction = 1 + random.Next(3);
wireLength = lengthWeights[random.Next(35)];
x = placement[0];
if(placement[1] < 5){
y = random.Next(5);
while(y == placement[1]){
y = random.Next(5);
}
}
else{
y = 5 + random.Next(4);
while(y == placement[1]){
y = 5 + random.Next(4);
}
}
}
}
if(!overlaps){
int[] middleWire = new int[4] {x, y, wireLength, direction};
Debug.Log("Placing x: " + x.ToString() + ", y: " + y.ToString() + ", length: " + wireLength.ToString() + ", direction: " + direction.ToString() + "Termination: " +placement[0].ToString() + ", " + placement[1].ToString());
circuit.Add(middleWire);
}
// new random wire placement from last termination point
x = placement[0];
if(placement[1] < 5){
y = random.Next(5);
while(y == placement[1]){
y = random.Next(5);
}
}
else{
y = 5 + random.Next(4);
while(y == placement[1]){
y = 5 + random.Next(4);
}
}
direction = 1 + random.Next(3);
wireLength = 2 + random.Next(9);
}
}
Debug.Log("FOUND PATH?");
Debug.Log(foundPath);
return circuit;
}
/// <inheritdoc/>
protected override void OnAwake()
{
breadboardContainer = new GameObject("Breadboard");
breadboardContainer.transform.parent = scenario.transform; // transform relative to the fixed length scenario
breadboardCache = new GameObjectOneWayCache(
breadboardContainer.transform, breadboard.categories.Select(element => element.Item1).ToArray());
edgeContainer = new GameObject("Edge Wires");
edgeContainer.transform.parent = scenario.transform; // transform relative to the fixed length scenario
edgeCache = new GameObjectOneWayCache(
edgeContainer.transform, edgeWires.categories.Select(element => element.Item1).ToArray());
middleContainer = new GameObject("Middle Wires");
middleContainer.transform.parent = scenario.transform; // transform relative to the fixed length scenario
middleCache = new GameObjectOneWayCache(
middleContainer.transform, middleWires.categories.Select(element => element.Item1).ToArray());
// Add the wires to the lists representing the wires allowed for certain positions
for (var i = 0; i < edgeWires.GetCategoryCount(); i++){
GameObject wire = edgeWires.GetCategory(i);
if(wire.name != "Edge7mm"){ //7mm wire doesn't reach the middle holes from the outside power rail
outsideRailList.Add(wire);
}
if(wire.name != "Edge17mm"){ //17mm wire doesn't leave room on the middle vertical bus
insideRailList.Add(wire);
}
}
}
/// <summary>
/// Generates a foreground layer of wire and resistor objects at the start of each scenario iteration
/// The objects form a connected circuit on the breadboard
/// </summary>
protected override void OnIterationStart()
{
List<int[]> circuit = findCircuit();
placeCircuit(circuit);
}
/// <summary>
/// Deletes generated foreground objects after each scenario iteration is complete
/// </summary>
protected override void OnIterationEnd()
{
breadboardCache.ResetAllObjects();
edgeCache.ResetAllObjects();
middleCache.ResetAllObjects();
}
}