Skip to content

Commit 480593e

Browse files
authored
Merge pull request #58 from Itangalo/1.1
1.1
2 parents fb3fef5 + 1c79f16 commit 480593e

21 files changed

Lines changed: 1850 additions & 305 deletions

000-simulation.gs

Lines changed: 18 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -11,42 +11,16 @@
1111
*/
1212

1313
// Initiate some global variables.
14-
var global = {
15-
defaults: {
16-
module: 'example1',
17-
deck: {
18-
shuffleWhenCreated: true,
19-
addDiscardWhenShuffling: true,
20-
displaySize: 0,
21-
autoFillDisplay: true,
22-
},
23-
track: {
24-
assumePresent: true,
25-
startSpaceId: false,
26-
loop: false,
27-
gridMovement: false,
28-
symmetricConnections: true,
29-
},
30-
market: {
31-
restockOnlyIncreases: true,
32-
},
33-
goods: {
34-
quantity: Number.POSITIVE_INFINITY,
35-
maxQuantity: Number.POSITIVE_INFINITY,
36-
},
37-
diceRoll: {
38-
quantity: 3,
39-
numberOfSides: 6,
40-
customSides: false,
41-
},
42-
},
43-
}; // Further populated by buildInitialData().
14+
var global = {}; // Further populated by buildInitialData().
4415
var modules = {}; // Populated by custom code.
45-
var module = global.defaults.module;
16+
var module;
4617
var gameState = {};
4718
global.startTime = Date.now();
4819

4920
function simulate(iterations = false, mod = false) {
21+
// Set global default values.
22+
setInitialDefaults();
23+
5024
// Set which module (game simulation) to run.
5125
if (mod !== false)
5226
module = mod;
@@ -55,18 +29,18 @@ function simulate(iterations = false, mod = false) {
5529
let gameStateSeed = modules[module].buildInitialData();
5630
log('Initial data complete.', 'system');
5731

58-
// Variable used to save data from each game iteration.
59-
let results = [];
60-
// Start iterating game plays.
32+
/**
33+
* Start iterating game plays.
34+
*/
35+
let results = []; // Variable used to save data from each game iteration.
6136
if (!iterations)
6237
iterations = global.defaults.iterations;
6338
for (let iteration = 1; iteration <= iterations; iteration++) {
64-
log('Starting iteration ' + iteration, 'system');
65-
gameState = copy(gameStateSeed);
66-
6739
/**
6840
* Set up each game.
6941
*/
42+
log('Starting iteration ' + iteration, 'system');
43+
gameState = copy(gameStateSeed);
7044
// Set up agents, if any. Note that these are stored in an array,
7145
// not keyed by id, to allow setting and changing order.
7246
if (gameState.agents) {
@@ -85,7 +59,7 @@ function simulate(iterations = false, mod = false) {
8559
if (gameState.tracks) {
8660
delete (gameState.tracks);
8761
for (let o of gameStateSeed.tracks) {
88-
new Track(o.track, o.spaces);
62+
new Track(o.track, o.spaces, o.pawns);
8963
}
9064
}
9165
if (gameState.markets) {
@@ -98,7 +72,9 @@ function simulate(iterations = false, mod = false) {
9872
// Make any customized additional processing of the game state.
9973
modules[module].preIteration();
10074

101-
// Play the game until it is over.
75+
/**
76+
* Play the game until it is over.
77+
*/
10278
gameState.round = 0;
10379
log('Starting first round in iteration ' + iteration, 'system');
10480
while (!modules[module].gameOver()) {
@@ -113,5 +89,8 @@ function simulate(iterations = false, mod = false) {
11389
results.push(modules[module].buildStatistics());
11490
}
11591

92+
/**
93+
* Display the processed results.
94+
*/
11695
return processResults(results);
11796
}

003-defaults.gs

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/**
2+
* @file: Contains default settings, as well as a function for applying the default settings.
3+
*/
4+
5+
function setInitialDefaults() {
6+
global.defaults = {
7+
module: 'example1',
8+
iterations: 100,
9+
logging : {
10+
categories: {
11+
rounds: true,
12+
notice: true,
13+
example: true,
14+
statistics: true,
15+
tests: true,
16+
system: true,
17+
errors: true,
18+
},
19+
showTimestamps: true,
20+
},
21+
statistics: {
22+
percentiles: [0, .05, .15, .50, .85, .95, 1],
23+
},
24+
deck: {
25+
shuffleWhenCreated: true,
26+
addDiscardWhenShuffling: true,
27+
displaySize: 0,
28+
autoFillDisplay: true,
29+
},
30+
track: {
31+
assumePresent: true,
32+
startSpaceId: false,
33+
loop: false,
34+
gridMovement: false,
35+
symmetricConnections: true,
36+
lineOfSightStepFraction: .1,
37+
},
38+
market: {
39+
restockOnlyIncreases: true,
40+
},
41+
goods: {
42+
quantity: Number.POSITIVE_INFINITY,
43+
maxQuantity: Number.POSITIVE_INFINITY,
44+
},
45+
diceRoll: {
46+
quantity: 3,
47+
numberOfSides: 6,
48+
customSides: false,
49+
},
50+
};
51+
// Apply some defaults right away.
52+
module = global.defaults.module;
53+
for (let i of ['logging', 'statistics'])
54+
global[i] = global.defaults[i];
55+
}

200-helpersGeneral.gs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -477,6 +477,20 @@ function getHighestProperty(obj) {
477477
return selectRandom(properties);
478478
}
479479

480+
// Returns the cartesian distance between point A and B, using the coordinates provided
481+
// in the 'coordinates' array. If B is false, distance to the origin (0) will be returned.
482+
function getDistance(pointA, pointB = false, coordinates) {
483+
if (!pointB) {
484+
pointB = {};
485+
for (let c in coordinates)
486+
pointB[c] = 0;
487+
}
488+
let a = 0;
489+
for (let c of coordinates)
490+
a += Math.pow(pointA[c] - pointB[c], 2);
491+
return Math.sqrt(a);
492+
}
493+
480494
/**
481495
* Internally used helper functions.
482496
*/

210-processResults.gs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ function processResults(results) {
3131
// Header row
3232
let message = 'DISTRIBUTION: average (percentile ';
3333
let values = [];
34-
for (let p of global.percentilesForStatistics) {
34+
for (let p of global.statistics.percentiles) {
3535
values.push(p);
3636
}
3737
message += values.join(' | ') + ')\r\n---\r\n';
@@ -40,7 +40,7 @@ function processResults(results) {
4040
message += i + ': ';
4141
message += average(sortedResults[i]).toFixed(2) + ' (';
4242
values = [];
43-
for (let p of global.percentilesForStatistics) {
43+
for (let p of global.statistics.percentiles) {
4444
values.push(percentile(sortedResults[i], p).toFixed(2));
4545
}
4646
message += values.join(' | ') + ') Non-zero at ';
@@ -67,7 +67,7 @@ function processResults(results) {
6767
output[2].push(getNonZeroThreshold(sortedResults[i]));
6868
}
6969
// Percentiles
70-
for (let p of global.percentilesForStatistics) {
70+
for (let p of global.statistics.percentiles) {
7171
let line = ['percentile ' + p];
7272
for (let i in sortedResults) {
7373
line.push(percentile(sortedResults[i], p));

220-logging.gs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
* @param {string} type: The type of log message, used for restricting actual output.
1010
*/
1111
function log(message, type) {
12-
if (!global.logging || global.logging.categories[type]) {
13-
if (!global.logging || global.logging.showTimestamps)
12+
if (global.logging.categories[type]) {
13+
if (global.logging.showTimestamps)
1414
message += ' (time: ' + (Date.now() - global.startTime) + ')';
1515
Logger.log(message);
1616
}

0 commit comments

Comments
 (0)