-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path000-simulation.gs
More file actions
105 lines (93 loc) · 3.04 KB
/
000-simulation.gs
File metadata and controls
105 lines (93 loc) · 3.04 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
/**
* Bot Playtesting Toolkit
*
* See https://github.com/Itangalo/Bot-Playtesting-Toolkit for details,
* including license (GNU GPL version 3) and issue queue.
*/
/**
* @file: Runs simulation. Creates some global variables.
* This file must be the first in the list of files when the script runs.
*/
// Initiate some global variables.
var BPTstatic = {}; // Further populated by buildInitialData().
var modules = {}; // Populated by custom code.
var module;
var gameState = {};
BPTstatic.startTime = Date.now();
function simulate(iterations = false, mod = false) {
// Set global default values.
setInitialDefaults();
// Set which module (game simulation) to run.
if (mod !== false)
module = mod;
let extraArguments = parseArguments(arguments, 2);
log('Starting to build initial data.', 'system');
let gameStateSeed = {};
if (modules[module].buildInitialData)
gameStateSeed = modules[module].buildInitialData(...extraArguments);
log('Initial data complete.', 'system');
/**
* Start iterating game plays.
*/
let results = []; // Variable used to save data from each game iteration.
if (!iterations)
iterations = BPTstatic.defaults.iterations;
for (let iteration = 1; iteration <= iterations; iteration++) {
/**
* Set up each game.
*/
log('Starting iteration ' + iteration, 'system');
gameState = copy(gameStateSeed);
// Set up agents, if any. Note that these are stored in an array,
// not keyed by id, to allow setting and changing order.
if (gameState.agents) {
delete (gameState.agents);
for (let a of gameStateSeed.agents) {
new Agent(a);
}
}
// Set up any decks, tracks and markets.
if (gameState.decks) {
delete (gameState.decks);
for (let o of gameStateSeed.decks) {
new Deck(o.deck, o.cards);
}
}
if (gameState.tracks) {
delete (gameState.tracks);
for (let o of gameStateSeed.tracks) {
new Track(o.track, o.spaces, o.pawns);
}
}
if (gameState.markets) {
delete (gameState.markets);
for (let o of gameStateSeed.markets) {
new Market(o.market, o.goods);
}
}
// Only on the first iteration, run postBuild().
if (iteration == 1 && modules[module].postBuild)
modules[module].postBuild(...extraArguments);
// Make any customized additional processing of the game state.
if (modules[module].preIteration)
modules[module].preIteration(...extraArguments);
/**
* Play the game until it is over.
*/
gameState.round = 0;
log('Starting first round in iteration ' + iteration, 'system');
while (!modules[module].gameOver(...extraArguments)) {
gameState.round++;
// Call the function carrying out the actual actions in a round.
modules[module].playRound(...extraArguments);
}
/**
* Process data that should be stored for statistics.
*/
results.push(modules[module].buildStatistics(...extraArguments));
}
/**
* Display the processed results.
*/
return processResults(results);
}