-
-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathgenerator_v3.js
335 lines (295 loc) · 8.76 KB
/
generator_v3.js
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
// Used in stable v4 -- should be copied as generator_v4 to introduce goal generation changes in v5
var generator_v3 = function(layout, difficulty, bingoList)
{
var amountOfVeryHard;
var amountOfHard;
var amountOfMedium;
var amountOfEasy;
var currentSheet = [];
var sheetLayout = [];
if (layout == "set")
{
sheetLayout = [ 1, 2, 0, 2, 1,
2, 0, 1, 0, 2,
0, 1, 3, 1, 0,
2, 0, 1, 0, 2,
1, 2, 0, 2, 1];
}
else if (layout == "random")
{
sheetLayout = [ 0, 0, 0, 0, 0,
0, 0, 0, 0, 0,
0, 0, 0, 0, 0,
0, 0, 0, 0, 0,
0, 0, 0, 0, 0];
switch(difficulty)
{
// Easy with some Very Easy
case 2:
amountOfVeryHard = 0;
amountOfHard = 0;
amountOfMedium = 0;
amountOfEasy = getRandomInt(15, 19);
break;
// Medium with some Easy
case 3:
amountOfVeryHard = 0;
amountOfHard = 0;
amountOfMedium = getRandomInt(15, 19);
amountOfEasy = 25 - amountOfMedium;
break;
// Hard with some Medium
case 4:
amountOfVeryHard = 0;
amountOfHard = getRandomInt(15, 19);
amountOfMedium = 25 - amountOfHard;
amountOfEasy = 25 - amountOfHard - amountOfMedium;
break;
// Very Hard with some Hard
case 5:
amountOfVeryHard = getRandomInt(15, 19);
amountOfHard = 25 - amountOfVeryHard;
amountOfMedium = 25 - amountOfHard - amountOfVeryHard;
amountOfEasy = 25 - amountOfHard - amountOfMedium- amountOfVeryHard;
break;
// Very Easy
default:
amountOfVeryHard = 0;
amountOfHard = 0;
amountOfMedium = 0;
amountOfEasy = 0;
}
function distributeDifficulty(amountOfDifficulty, difficulty)
{
for (var i = 0; i < amountOfDifficulty; i++)
{
var cont = true;
var failSafe = 0;
do
{
cont = true;
failSafe++;
var rng = Math.floor((Math.random() * 25));
if (sheetLayout[rng] == 0)
{
sheetLayout[rng] = difficulty;
}
else
{
cont = false;
if (failSafe >= 500)
{
break;
}
}
}
while (cont == false);
}
}
distributeDifficulty(amountOfVeryHard, 4);
distributeDifficulty(amountOfHard, 3);
distributeDifficulty(amountOfMedium, 2);
distributeDifficulty(amountOfEasy, 1);
}
// Shuffle the sheet pre generation to allow for accurate line checks.
// Sheet must be shuffled to avoid hard to place goals being more likely to be top left than bottom right
var indexes = Array.from(Array(25).keys());
shuffle(indexes);
// Keep track off what tags, antisynergys, reactants and catalysts are already on the sheet
var tagCount = {};
var antisynergys = new Set(),
reactants = new Set(),
catalysts = new Set();
// Try to generate 25 goals to populate the sheet
for (var i=0; i<=24; i++)
{
// Keep track of how many times we've tried to generate a goal
var failSafe = 0;
GoalGen:
do
{
failSafe++;
// Generate a new goal candidate from the list of goals
var rng = Math.floor((Math.random() * bingoList[sheetLayout[i]].length - 1) + 1);
var goalCandidate = bingoList[sheetLayout[i]][rng];
// Check if the goal has an infrequency modifier
if (typeof goalCandidate.infrequency !== 'undefined')
{
// If it does, make it less likely to appear based on the value of infrequency
if (Math.floor((Math.random() * goalCandidate.infrequency) + 1) < goalCandidate.infrequency)
{
/*
* "infrequency" value stores how less likely a goal is. E.g. infrequency == 25
* makes a goal 1/25 (4%) as likely as a goal with infrequency == 1.
*/
//console.log("cont = false, infrequency check failed");
// If we failed the RNG roll, continue the do while loop and try again
continue GoalGen;
}
}
// If the current sheet already has this goal on it
if (currentSheet.some(r=> r.name === goalCandidate.name))
{
// Get a new goal
console.log(goalCandidate.name + " already on the board");
continue GoalGen;
}
// Check if the goal has any tags
if (goalCandidate.tags != null)
{
// foreach tag in the goal's tags
for (const tag of goalCandidate.tags)
{
// If the tag isn't in our list of tags yet, add it and set it to 0
if (typeof tagCount[tag.name] == 'undefined')
{
//console.log(tag.name + " not collected yet, adding");
tagCount[tag.name] = 0;
}
// Otherwise check if it's higher than it should be
else if (tagCount[tag.name] >= tag.max[difficulty - 1])
{
// If we've got too many of that tag, get a new goal
console.log(tag.name + " max reached with " + tagCount[tag.name] + " on the board");
continue GoalGen;
}
}
// If the goal candidate contains tags that cannot be on the same line as other goals with that tag
if (goalCandidate.tags.some(r=> r.line == false))
{
// Go through every goal currently on the sheet
for (var z=0; z < i; z++)
{
// If the currentSheet goal has tags AND are on the same line
if (currentSheet[indexes[z]].tags != null && isOnSameLine(indexes[i], indexes[z]))
{
// If both goals have the same tag
if (currentSheet[indexes[z]].tags.some(r=> r.line == false && goalCandidate.tags.some(s=> r.name === s.name)))
{
console.log("Cannot be on same line: " + goalCandidate.name + " and " + currentSheet[indexes[z]].name);
continue GoalGen;
}
}
}
}
}
// Check if the goal (and the goals on the sheet) has any antisynergies
if (typeof goalCandidate.antisynergy !== 'undefined')
{
// If it does, check to see if it's already on the sheet
if (goalCandidate.antisynergy.some(a => antisynergys.has(a)))
{
// If it is, get a new goal
console.log("antisynergy for: " + goalCandidate.name);
continue GoalGen;
}
}
// Check if the goal generated is a catalyst for anything already on the sheet
if (typeof goalCandidate.catalyst !== 'undefined')
{
if (goalCandidate.catalyst.some(c => reactants.has(c)))
{
// If it is, get a new goal
console.log("reactants for: " + goalCandidate.name);
continue GoalGen;
}
}
// Check if the goal generated is a reactant for anything already on the sheet
if (typeof goalCandidate.reactant !== 'undefined')
{
// If it does, check to see if it's already on the sheet
if (goalCandidate.reactant.some(r => catalysts.has(r)))
{
// If it is, get a new goal
console.log("catalyst for: " + goalCandidate.name);
continue GoalGen;
}
}
// If the loop is stuck because no more suitable goals
if (failSafe >= 500)
{
// Check for a non-broken goal list
if (sheetLayout[i] == 0)
{
window.alert("Invalid Goal List");
break GoalGen;
}
// Move the difficulty down by one
sheetLayout[i]--;
failSafe = 0;
console.log("failSafe occurred on " + (i + 1) + "/25, reducing goal difficulty to " + sheetLayout[i]);
continue GoalGen;
}
// If we made it this far, the goal must be good to go
break GoalGen;
}
while (true);
// We successfully picked a goal, add its tags to tagCount
for (const tag of goalCandidate.tags)
{
tagCount[tag.name]++;
//console.log(tagCount);
}
// Add its antisynergys to the set of antisynergys
if (typeof goalCandidate.antisynergy !== 'undefined')
{
goalCandidate.antisynergy.forEach(a => antisynergys.add(a));
}
// Add its catalysts to the set of catalysts
if (typeof goalCandidate.catalyst !== 'undefined')
{
goalCandidate.catalyst.forEach(c => catalysts.add(c));
}
// Add its reactants to the set of reactants
if (typeof goalCandidate.reactant !== 'undefined')
{
goalCandidate.reactant.forEach(r => reactants.add(r));
}
// Add the goal to the JSON list of goals
var goal = JSON.parse(JSON.stringify(goalCandidate)); // Clone object
// Replace random ranges in goal name
goal.generatedName = goal.name.replace(/\((\d+)-(\d+)\)/g, function(match, n1, n2, offset, input)
{
n1 = parseInt(n1);
n2 = parseInt(n2);
return getRandomInt(n1, n2);
});
// Add the sheet to the goal
currentSheet[indexes[i]] = goal;
}
//shuffle(currentSheet); Old Sheet Shuffling, now shuffling indexes pre-generation instead
console.log("Completed sheet generation");
return currentSheet;
}
function isOnSameLine(a, b)
{
const secondaryDiagonal = [4, 8, 12, 16, 20];
// Top Left -> Bottom Right Diagonal
if (a % 6 == 0 && b % 6 == 0)
{
return true;
}
// Top Right -> Bottom left Diagonal
else if (secondaryDiagonal.includes(a) && secondaryDiagonal.includes(b))
{
return true;
}
// Rows
else if (Math.floor(a / 5) == Math.floor(b / 5))
{
return true;
}
// Columns
else if (a % 5 == b % 5)
{
return true;
}
return false;
}
function shuffle(a) {
for (let i = a.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[a[i], a[j]] = [a[j], a[i]];
}
return a;
}