@jutge.org/toolkit 4.2.22 → 4.2.24
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/assets/problems/games/the-walking-dead.pbm/README.md +12 -0
- package/assets/problems/games/the-walking-dead.pbm/ca/Doc/Makefile +6 -0
- package/assets/problems/games/the-walking-dead.pbm/ca/Doc/api.tex +246 -0
- package/assets/problems/games/the-walking-dead.pbm/ca/Doc/defs.tex +2 -0
- package/assets/problems/games/the-walking-dead.pbm/ca/Doc/main.tex +63 -0
- package/assets/problems/games/the-walking-dead.pbm/ca/Doc/programming.tex +452 -0
- package/assets/problems/games/the-walking-dead.pbm/ca/Doc/rules.tex +222 -0
- package/assets/problems/games/the-walking-dead.pbm/ca/Doc/screenshot.png +0 -0
- package/assets/problems/games/the-walking-dead.pbm/ca/Doc/tips.tex +81 -0
- package/assets/problems/games/the-walking-dead.pbm/ca/Doc/twd.png +0 -0
- package/assets/problems/games/the-walking-dead.pbm/ca/Doc/viewer.tex +41 -0
- package/assets/problems/games/the-walking-dead.pbm/ca/Doc-eng/Makefile +6 -0
- package/assets/problems/games/the-walking-dead.pbm/ca/Doc-eng/api.tex +246 -0
- package/assets/problems/games/the-walking-dead.pbm/ca/Doc-eng/defs.tex +2 -0
- package/assets/problems/games/the-walking-dead.pbm/ca/Doc-eng/main.tex +63 -0
- package/assets/problems/games/the-walking-dead.pbm/ca/Doc-eng/programming.tex +176 -0
- package/assets/problems/games/the-walking-dead.pbm/ca/Doc-eng/rules.tex +194 -0
- package/assets/problems/games/the-walking-dead.pbm/ca/Doc-eng/screenshot.png +0 -0
- package/assets/problems/games/the-walking-dead.pbm/ca/Doc-eng/tips.tex +85 -0
- package/assets/problems/games/the-walking-dead.pbm/ca/Doc-eng/twd.png +0 -0
- package/assets/problems/games/the-walking-dead.pbm/ca/Doc-eng/viewer.tex +36 -0
- package/assets/problems/games/the-walking-dead.pbm/ca/Obj/AIDummy.o.Linux64 +0 -0
- package/assets/problems/games/the-walking-dead.pbm/ca/Obj/AIDummy.o.Linux64.Debug +0 -0
- package/assets/problems/games/the-walking-dead.pbm/ca/Obj/AIDummy.o.MacOS +0 -0
- package/assets/problems/games/the-walking-dead.pbm/ca/Obj/AIDummy.o.MacOS.ARM +0 -0
- package/assets/problems/games/the-walking-dead.pbm/ca/Obj/AIDummy.o.MacOS.ARM.Debug +0 -0
- package/assets/problems/games/the-walking-dead.pbm/ca/Obj/AIDummy.o.MacOS.Debug +0 -0
- package/assets/problems/games/the-walking-dead.pbm/ca/Obj/Board.o.Linux64 +0 -0
- package/assets/problems/games/the-walking-dead.pbm/ca/Obj/Board.o.Linux64.Debug +0 -0
- package/assets/problems/games/the-walking-dead.pbm/ca/Obj/Board.o.MacOS +0 -0
- package/assets/problems/games/the-walking-dead.pbm/ca/Obj/Board.o.MacOS.ARM +0 -0
- package/assets/problems/games/the-walking-dead.pbm/ca/Obj/Board.o.MacOS.ARM.Debug +0 -0
- package/assets/problems/games/the-walking-dead.pbm/ca/Obj/Board.o.MacOS.Debug +0 -0
- package/assets/problems/games/the-walking-dead.pbm/ca/Runner/AIDemo.cc +89 -0
- package/assets/problems/games/the-walking-dead.pbm/ca/Runner/AIDummy.cc +202 -0
- package/assets/problems/games/the-walking-dead.pbm/ca/Runner/AINull.cc +37 -0
- package/assets/problems/games/the-walking-dead.pbm/ca/Runner/Action.cc +34 -0
- package/assets/problems/games/the-walking-dead.pbm/ca/Runner/Action.hh +107 -0
- package/assets/problems/games/the-walking-dead.pbm/ca/Runner/Board.cc +975 -0
- package/assets/problems/games/the-walking-dead.pbm/ca/Runner/Board.hh +287 -0
- package/assets/problems/games/the-walking-dead.pbm/ca/Runner/Defs.hh +2 -0
- package/assets/problems/games/the-walking-dead.pbm/ca/Runner/Game.cc +55 -0
- package/assets/problems/games/the-walking-dead.pbm/ca/Runner/Game.hh +23 -0
- package/assets/problems/games/the-walking-dead.pbm/ca/Runner/Info.cc +174 -0
- package/assets/problems/games/the-walking-dead.pbm/ca/Runner/Info.hh +129 -0
- package/assets/problems/games/the-walking-dead.pbm/ca/Runner/Main.cc +83 -0
- package/assets/problems/games/the-walking-dead.pbm/ca/Runner/Makefile +56 -0
- package/assets/problems/games/the-walking-dead.pbm/ca/Runner/Player.cc +66 -0
- package/assets/problems/games/the-walking-dead.pbm/ca/Runner/Player.hh +63 -0
- package/assets/problems/games/the-walking-dead.pbm/ca/Runner/README.txt +9 -0
- package/assets/problems/games/the-walking-dead.pbm/ca/Runner/Random.cc +3 -0
- package/assets/problems/games/the-walking-dead.pbm/ca/Runner/Random.hh +88 -0
- package/assets/problems/games/the-walking-dead.pbm/ca/Runner/Registry.cc +28 -0
- package/assets/problems/games/the-walking-dead.pbm/ca/Runner/Registry.hh +42 -0
- package/assets/problems/games/the-walking-dead.pbm/ca/Runner/SecGame.cc +368 -0
- package/assets/problems/games/the-walking-dead.pbm/ca/Runner/SecGame.hh +90 -0
- package/assets/problems/games/the-walking-dead.pbm/ca/Runner/SecMain.cc +99 -0
- package/assets/problems/games/the-walking-dead.pbm/ca/Runner/Settings.cc +67 -0
- package/assets/problems/games/the-walking-dead.pbm/ca/Runner/Settings.hh +175 -0
- package/assets/problems/games/the-walking-dead.pbm/ca/Runner/State.cc +3 -0
- package/assets/problems/games/the-walking-dead.pbm/ca/Runner/State.hh +188 -0
- package/assets/problems/games/the-walking-dead.pbm/ca/Runner/Structs.cc +3 -0
- package/assets/problems/games/the-walking-dead.pbm/ca/Runner/Structs.hh +377 -0
- package/assets/problems/games/the-walking-dead.pbm/ca/Runner/Utils.cc +3 -0
- package/assets/problems/games/the-walking-dead.pbm/ca/Runner/Utils.hh +77 -0
- package/assets/problems/games/the-walking-dead.pbm/ca/Runner/default-fixed.cnf +120 -0
- package/assets/problems/games/the-walking-dead.pbm/ca/Runner/default.cnf +17 -0
- package/assets/problems/games/the-walking-dead.pbm/ca/Viewer/css/ui-lightness/images/ui-bg_diagonals-thick_18_b81900_40x40.png +0 -0
- package/assets/problems/games/the-walking-dead.pbm/ca/Viewer/css/ui-lightness/images/ui-bg_diagonals-thick_20_666666_40x40.png +0 -0
- package/assets/problems/games/the-walking-dead.pbm/ca/Viewer/css/ui-lightness/images/ui-bg_flat_10_000000_40x100.png +0 -0
- package/assets/problems/games/the-walking-dead.pbm/ca/Viewer/css/ui-lightness/images/ui-bg_glass_100_f6f6f6_1x400.png +0 -0
- package/assets/problems/games/the-walking-dead.pbm/ca/Viewer/css/ui-lightness/images/ui-bg_glass_100_fdf5ce_1x400.png +0 -0
- package/assets/problems/games/the-walking-dead.pbm/ca/Viewer/css/ui-lightness/images/ui-bg_glass_65_ffffff_1x400.png +0 -0
- package/assets/problems/games/the-walking-dead.pbm/ca/Viewer/css/ui-lightness/images/ui-bg_gloss-wave_35_f6a828_500x100.png +0 -0
- package/assets/problems/games/the-walking-dead.pbm/ca/Viewer/css/ui-lightness/images/ui-bg_highlight-soft_100_eeeeee_1x100.png +0 -0
- package/assets/problems/games/the-walking-dead.pbm/ca/Viewer/css/ui-lightness/images/ui-bg_highlight-soft_75_ffe45c_1x100.png +0 -0
- package/assets/problems/games/the-walking-dead.pbm/ca/Viewer/css/ui-lightness/images/ui-icons_222222_256x240.png +0 -0
- package/assets/problems/games/the-walking-dead.pbm/ca/Viewer/css/ui-lightness/images/ui-icons_228ef1_256x240.png +0 -0
- package/assets/problems/games/the-walking-dead.pbm/ca/Viewer/css/ui-lightness/images/ui-icons_ef8c08_256x240.png +0 -0
- package/assets/problems/games/the-walking-dead.pbm/ca/Viewer/css/ui-lightness/images/ui-icons_ffd27a_256x240.png +0 -0
- package/assets/problems/games/the-walking-dead.pbm/ca/Viewer/css/ui-lightness/images/ui-icons_ffffff_256x240.png +0 -0
- package/assets/problems/games/the-walking-dead.pbm/ca/Viewer/css/ui-lightness/jquery-ui-1.8.18.custom.css +310 -0
- package/assets/problems/games/the-walking-dead.pbm/ca/Viewer/help.html +18 -0
- package/assets/problems/games/the-walking-dead.pbm/ca/Viewer/img/but_close.png +0 -0
- package/assets/problems/games/the-walking-dead.pbm/ca/Viewer/img/but_end.png +0 -0
- package/assets/problems/games/the-walking-dead.pbm/ca/Viewer/img/but_help.png +0 -0
- package/assets/problems/games/the-walking-dead.pbm/ca/Viewer/img/but_pause.png +0 -0
- package/assets/problems/games/the-walking-dead.pbm/ca/Viewer/img/but_play.png +0 -0
- package/assets/problems/games/the-walking-dead.pbm/ca/Viewer/img/but_refresh.png +0 -0
- package/assets/problems/games/the-walking-dead.pbm/ca/Viewer/img/but_start.png +0 -0
- package/assets/problems/games/the-walking-dead.pbm/ca/Viewer/img/logo.png +0 -0
- package/assets/problems/games/the-walking-dead.pbm/ca/Viewer/js/jquery-1.7.1.min.js +4 -0
- package/assets/problems/games/the-walking-dead.pbm/ca/Viewer/js/jquery-ui-1.8.18.custom.min.js +49 -0
- package/assets/problems/games/the-walking-dead.pbm/ca/Viewer/sample.out +52807 -0
- package/assets/problems/games/the-walking-dead.pbm/ca/Viewer/viewer.html +118 -0
- package/assets/problems/games/the-walking-dead.pbm/ca/Viewer/viewer.js +846 -0
- package/assets/problems/games/the-walking-dead.pbm/ca/Viewer/viewer.sh +28 -0
- package/assets/problems/games/the-walking-dead.pbm/ca/handler.yml +6 -0
- package/assets/problems/games/the-walking-dead.pbm/ca/problem.ca.yml +3 -0
- package/assets/prompts/creators/create-solution.tpl.txt +15 -8
- package/assets/prompts/proglangs/cc.md +6 -2
- package/assets/prompts/proglangs/py.md +10 -6
- package/dist/index.js +370 -366
- package/docs/getting-started-guide.md +1 -1
- package/docs/install-linux.md +1 -1
- package/docs/install-macos.md +1 -1
- package/docs/install-windows.md +1 -1
- package/package.json +11 -11
- package/toolkit/ask.ts +0 -2
- package/toolkit/doctor.ts +1 -1
- package/toolkit/make.ts +2 -2
|
@@ -0,0 +1,846 @@
|
|
|
1
|
+
|
|
2
|
+
// *********************************************************************
|
|
3
|
+
// Global variables
|
|
4
|
+
// *********************************************************************
|
|
5
|
+
|
|
6
|
+
// Viewer state.
|
|
7
|
+
var gamePaused = true;
|
|
8
|
+
var gamePreview = false; // If true, render will be called for the next tick even if game is paused, and then will be set to false.
|
|
9
|
+
var gameAnim = true;
|
|
10
|
+
var gameDirection = 1;
|
|
11
|
+
var actRound = 0; // Current round index.
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
// Data.
|
|
15
|
+
var raw_data_str; // String for storing the raw data.
|
|
16
|
+
var dataLoaded = false; // Set to true when raw_data_str is ready to be parsed.
|
|
17
|
+
var data = { } // Object for storing all the game data.
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
// Animation.
|
|
21
|
+
var speed = 10; // Ticks per second.
|
|
22
|
+
var FRAMES_PER_ROUND = 2;
|
|
23
|
+
var frames = 0; // Incremented each tick, when it reaches FRAMES_PER_ROUND, actRound is updated (acording to gameDirection).
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
// Visuals.
|
|
27
|
+
var unitSize = 0.6; // 1 = same size as tile.
|
|
28
|
+
var unitLineWidth = 2;
|
|
29
|
+
var grid_color = "#000000";
|
|
30
|
+
var food_color = "#ff0000";
|
|
31
|
+
|
|
32
|
+
var cell_colors = {
|
|
33
|
+
'.': "#FFFFFF",
|
|
34
|
+
'0': "#F8A858",
|
|
35
|
+
'1': "#70FF70",
|
|
36
|
+
'2': "#60A8FF",
|
|
37
|
+
'3': "#C0A0FF",
|
|
38
|
+
'W': "#46466D",
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
var player_colors = {
|
|
43
|
+
0: "#B06020",
|
|
44
|
+
1: "#008000",
|
|
45
|
+
2: "#0050BB",
|
|
46
|
+
3: "#8000A0"
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
// *********************************************************************
|
|
51
|
+
// Utility functions
|
|
52
|
+
// *********************************************************************
|
|
53
|
+
|
|
54
|
+
function getURLParameter (name) {
|
|
55
|
+
// http://stackoverflow.com/questions/1403888/get-url-parameter-with-jquery
|
|
56
|
+
var a = (RegExp(name + '=' + '(.+?)(&|$)').exec(location.search)||[,null])[1]
|
|
57
|
+
if (a != null) return decodeURI(a);
|
|
58
|
+
return null;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
// Callback has a single parameter with the file contents.
|
|
63
|
+
function loadFile (file, callback) {
|
|
64
|
+
var xmlhttp;
|
|
65
|
+
|
|
66
|
+
if (file == null || file == "") {
|
|
67
|
+
alert("You must specify a file to load.");
|
|
68
|
+
return;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
if (window.XMLHttpRequest) xmlhttp = new XMLHttpRequest(); // Code for IE7+, Firefox, Chrome, Opera, Safari.
|
|
72
|
+
else xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); // Code for IE6, IE5.
|
|
73
|
+
|
|
74
|
+
// http://www.w3schools.com/ajax/ajax_xmlhttprequest_onreadystatechange.asp
|
|
75
|
+
xmlhttp.onreadystatechange = function() {
|
|
76
|
+
// Note: We cannot check xmlhttp.status != 200 for errors because status is not set when loading local files.
|
|
77
|
+
if (xmlhttp.readyState == 4) callback(xmlhttp.responseText);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
xmlhttp.open("GET", file, false);
|
|
81
|
+
xmlhttp.send();
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
function int (s) {
|
|
86
|
+
return parseInt(s);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
function double (s) {
|
|
91
|
+
return parseFloat(s);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
function parse_assert (read_value, expected_value) {
|
|
96
|
+
var correct = (read_value == expected_value);
|
|
97
|
+
if (!correct) alert("Error parsing file, expected token: " + expected_value + ", read token: " + read_value + ".");
|
|
98
|
+
return correct;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
// *********************************************************************
|
|
103
|
+
// Initialization functions
|
|
104
|
+
// *********************************************************************
|
|
105
|
+
|
|
106
|
+
function parseData (raw_data_str) {
|
|
107
|
+
if (raw_data_str == "") {
|
|
108
|
+
alert("Could not load game file.");
|
|
109
|
+
return false;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
// Convert text to tokens.
|
|
113
|
+
var st = raw_data_str + "";
|
|
114
|
+
var t = st.replace('\n', ' ').split(/\s+/);
|
|
115
|
+
var p = 0;
|
|
116
|
+
|
|
117
|
+
data.secgame = (t[p++] == "SecGame");
|
|
118
|
+
|
|
119
|
+
parse_assert(t[p++], "Seed");
|
|
120
|
+
data.seed = int(t[p++]);
|
|
121
|
+
|
|
122
|
+
// Game and version.
|
|
123
|
+
if (t[p++] != "TheWalkingDead") {
|
|
124
|
+
alert("Are you sure this is a The WalkingDead game file?");
|
|
125
|
+
document.getElementById('file').value = "";
|
|
126
|
+
document.getElementById('inputdiv').style.display = "";
|
|
127
|
+
document.getElementById('loadingdiv').style.display = "none";
|
|
128
|
+
return false;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
data.version = t[p++];
|
|
132
|
+
if (data.version != "1.0") alert("Unsupported game version! Trying to load it anyway.");
|
|
133
|
+
|
|
134
|
+
parse_assert(t[p++], "NUM_PLAYERS");
|
|
135
|
+
data.nb_players = int(t[p++]);
|
|
136
|
+
|
|
137
|
+
parse_assert(t[p++], "NUM_ROUNDS");
|
|
138
|
+
data.nb_rounds = int(t[p++]);
|
|
139
|
+
|
|
140
|
+
parse_assert(t[p++], "BOARD_ROWS");
|
|
141
|
+
data.rows = int(t[p++]);
|
|
142
|
+
|
|
143
|
+
parse_assert(t[p++], "BOARD_COLS");
|
|
144
|
+
data.cols = int(t[p++]);
|
|
145
|
+
|
|
146
|
+
parse_assert(t[p++], "NUM_INI_UNITS_PER_CLAN");
|
|
147
|
+
data.nb_ini_units_clan = int(t[p++]);
|
|
148
|
+
|
|
149
|
+
parse_assert(t[p++], "NUM_INI_ZOMBIES");
|
|
150
|
+
data.nb_ini_zombies = int(t[p++]);
|
|
151
|
+
|
|
152
|
+
parse_assert(t[p++], "NUM_INI_FOOD");
|
|
153
|
+
data.nb_ini_food = int(t[p++]);
|
|
154
|
+
|
|
155
|
+
parse_assert(t[p++], "CLAN_INI_STRENGTH");
|
|
156
|
+
data.ini_strength = int(t[p++]);
|
|
157
|
+
|
|
158
|
+
parse_assert(t[p++], "POINTS_FOR_KILLING_PERSON");
|
|
159
|
+
data.pts_kill_person = int(t[p++]);
|
|
160
|
+
|
|
161
|
+
parse_assert(t[p++], "POINTS_FOR_KILLING_ZOMBIE");
|
|
162
|
+
data.pts_kill_zombie = int(t[p++]);
|
|
163
|
+
|
|
164
|
+
parse_assert(t[p++], "POINTS_PER_OWNED_CELL");
|
|
165
|
+
data.pts_owned_cell = int(t[p++]);
|
|
166
|
+
|
|
167
|
+
parse_assert(t[p++], "FOOD_STRENGTH");
|
|
168
|
+
data.food_strength = int(t[p++]);
|
|
169
|
+
|
|
170
|
+
parse_assert(t[p++], "ROUNDS_BEFORE_BECOMING_ZOMBIE");
|
|
171
|
+
data.rounds_for_zombie = int(t[p++]);
|
|
172
|
+
|
|
173
|
+
|
|
174
|
+
data.nb_units = data.nb_players * data.nb_ini_units_clan + data.nb_ini_zombies;
|
|
175
|
+
|
|
176
|
+
parse_assert(t[p++], "names");
|
|
177
|
+
data.names = new Array();
|
|
178
|
+
for (var i = 0; i < data.nb_players; ++i) data.names[i] = t[p++];
|
|
179
|
+
|
|
180
|
+
data.rounds = new Array();
|
|
181
|
+
for (var round = 0; round <= data.nb_rounds; ++round) {
|
|
182
|
+
// Grid.
|
|
183
|
+
|
|
184
|
+
p++; // 1st line of column labels.
|
|
185
|
+
p++; // 2nd line of column labels.
|
|
186
|
+
|
|
187
|
+
data.rounds[round] = new Object();
|
|
188
|
+
data.rounds[round].rows = new Array();
|
|
189
|
+
for (var i = 0; i < data.rows; ++i) {
|
|
190
|
+
parse_assert(t[p++], i); // Row label.
|
|
191
|
+
data.rounds[round].rows[i] = t[p++]; // Row.
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
// Units.
|
|
195
|
+
parse_assert(t[p++], "units");
|
|
196
|
+
data.rounds[round].units = new Array();
|
|
197
|
+
data.rounds[round].units.length = int(t[p++]);
|
|
198
|
+
data.rounds[round].alive_units = new Array();
|
|
199
|
+
for (var i = 0; i < data.nb_players; ++i) {
|
|
200
|
+
data.rounds[round].alive_units[i] = 0;
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
parse_assert(t[p++], "type");
|
|
204
|
+
parse_assert(t[p++], "id");
|
|
205
|
+
parse_assert(t[p++], "player");
|
|
206
|
+
parse_assert(t[p++], "row");
|
|
207
|
+
parse_assert(t[p++], "column");
|
|
208
|
+
parse_assert(t[p++], "rounds");
|
|
209
|
+
for (var i = 0; i < data.rounds[round].units.length; ++i) {
|
|
210
|
+
data.rounds[round].units[i] = new Object();
|
|
211
|
+
data.rounds[round].units[i].type = t[p++]; // (a)live, (d)ead, (z)ombie
|
|
212
|
+
data.rounds[round].units[i].id = int(t[p++]);
|
|
213
|
+
data.rounds[round].units[i].player = int(t[p++]);
|
|
214
|
+
data.rounds[round].units[i].i = int(t[p++]);
|
|
215
|
+
data.rounds[round].units[i].j = int(t[p++]);
|
|
216
|
+
data.rounds[round].units[i].rounds = int(t[p++]);
|
|
217
|
+
if (data.rounds[round].units[i].type == 'a') {
|
|
218
|
+
data.rounds[round].alive_units[data.rounds[round].units[i].player]++;
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
// Food.
|
|
223
|
+
parse_assert(t[p++], "food");
|
|
224
|
+
data.rounds[round].food = new Array();
|
|
225
|
+
data.rounds[round].food.length = int(t[p++]);
|
|
226
|
+
parse_assert(t[p++], "row");
|
|
227
|
+
parse_assert(t[p++], "column");
|
|
228
|
+
for (var i = 0; i < data.rounds[round].food.length; ++i) {
|
|
229
|
+
data.rounds[round].food[i] = new Object();
|
|
230
|
+
data.rounds[round].food[i].i = int(t[p++]);
|
|
231
|
+
data.rounds[round].food[i].j = int(t[p++]);
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
// Round.
|
|
235
|
+
parse_assert(t[p++], "round");
|
|
236
|
+
if (int(t[p++]) != round) alert("Wrong round number!");
|
|
237
|
+
|
|
238
|
+
// Score.
|
|
239
|
+
parse_assert(t[p++], "score");
|
|
240
|
+
data.rounds[round].score = new Array();
|
|
241
|
+
for (var i = 0; i < data.nb_players; ++i) {
|
|
242
|
+
data.rounds[round].score[i] = int(t[p++]);
|
|
243
|
+
if (data.rounds[round].score[i] > data.max_score)
|
|
244
|
+
data.max_score = data.rounds[round].score[i];
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
// Score acc.
|
|
248
|
+
parse_assert(t[p++], "scr_acc");
|
|
249
|
+
data.rounds[round].score_acc = new Array();
|
|
250
|
+
for (var i = 0; i < data.nb_players; ++i) {
|
|
251
|
+
data.rounds[round].score_acc[i] = int(t[p++]);
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
// Strength.
|
|
255
|
+
parse_assert(t[p++], "strength");
|
|
256
|
+
data.rounds[round].strength = new Array();
|
|
257
|
+
for (var i = 0; i < data.nb_players; ++i) {
|
|
258
|
+
data.rounds[round].strength[i] = int(t[p++]);
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
// Status.
|
|
262
|
+
parse_assert(t[p++], "status");
|
|
263
|
+
data.rounds[round].cpu = new Array();
|
|
264
|
+
for (var i = 0; i < data.nb_players; ++i) {
|
|
265
|
+
var cpu = int(double(t[p++])*100);
|
|
266
|
+
data.rounds[round].cpu[i] = (cpu == -100) ? "out" : cpu + "%";
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
// Commands.
|
|
270
|
+
if (round != data.nb_rounds) {
|
|
271
|
+
parse_assert(t[p++], "commands");
|
|
272
|
+
data.rounds[round].commands = new Array();
|
|
273
|
+
data.rounds[round].commands.length = int(t[p++]);
|
|
274
|
+
for (var i = 0; i < data.rounds[round].commands.length; ++i) {
|
|
275
|
+
data.rounds[round].commands[i] = new Object();
|
|
276
|
+
data.rounds[round].commands[i].id = int(t[p++]);
|
|
277
|
+
data.rounds[round].commands[i].action = t[p++]; // (m)ove
|
|
278
|
+
data.rounds[round].commands[i].direction = t[p++]; // (u)p, (d)own, (r)ight, (l)eft and diagonals
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
return true;
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
|
|
286
|
+
// Initializing the game.
|
|
287
|
+
function initGame (raw_data) {
|
|
288
|
+
document.getElementById("loadingdiv").style.display = "";
|
|
289
|
+
|
|
290
|
+
// TODO: Next two calls could run concurrently.
|
|
291
|
+
if (parseData(raw_data) === false) return;
|
|
292
|
+
preloadImages();
|
|
293
|
+
|
|
294
|
+
gamePaused = false;
|
|
295
|
+
gamePreview = true;
|
|
296
|
+
|
|
297
|
+
// Canvas element.
|
|
298
|
+
canvas = document.getElementById('myCanvas');
|
|
299
|
+
context = canvas.getContext("2d");
|
|
300
|
+
|
|
301
|
+
// Prepare the slider.
|
|
302
|
+
$("#slider").slider({
|
|
303
|
+
slide: function(event, ui) {
|
|
304
|
+
var value = $("#slider").slider( "option", "value" );
|
|
305
|
+
actRound = value;
|
|
306
|
+
frames = 0;
|
|
307
|
+
gamePaused = true;
|
|
308
|
+
gamePreview = true;
|
|
309
|
+
}
|
|
310
|
+
});
|
|
311
|
+
$("#slider").width(600);
|
|
312
|
+
$("#slider").slider("option", {min: 0, max: data.nb_rounds});
|
|
313
|
+
|
|
314
|
+
// Set the listerners for interaction.
|
|
315
|
+
document.addEventListener('mousewheel', onDocumentMouseWheel, false);
|
|
316
|
+
document.addEventListener('keydown', onDocumentKeyDown, false);
|
|
317
|
+
document.addEventListener('keyup', onDocumentKeyUp, false);
|
|
318
|
+
|
|
319
|
+
window.addEventListener('resize', onWindowResize, false);
|
|
320
|
+
onWindowResize();
|
|
321
|
+
|
|
322
|
+
document.getElementById("loadingdiv").style.display = "none";
|
|
323
|
+
document.getElementById("gamediv").style.display = "";
|
|
324
|
+
|
|
325
|
+
mainloop();
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
|
|
329
|
+
function preloadImages () {
|
|
330
|
+
data.img = new Array();
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
|
|
334
|
+
// *********************************************************************
|
|
335
|
+
// Main loop functions
|
|
336
|
+
// *********************************************************************
|
|
337
|
+
|
|
338
|
+
function updateGame () {
|
|
339
|
+
$("#slider").slider("option", "value", actRound);
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
|
|
343
|
+
function writeGameState () {
|
|
344
|
+
// Write round.
|
|
345
|
+
$("#round").html("Round: " + actRound);
|
|
346
|
+
|
|
347
|
+
// Update scoreboard.
|
|
348
|
+
var scoreboard = "";
|
|
349
|
+
for (var i = 0; i < data.nb_players; ++i) {
|
|
350
|
+
str_uni = 0;
|
|
351
|
+
if (data.rounds[actRound].alive_units[i] != 0)
|
|
352
|
+
str_uni = int(data.rounds[actRound].strength[i]/data.rounds[actRound].alive_units[i]);
|
|
353
|
+
scoreboard += "<span class='score'>"
|
|
354
|
+
+ "<div style='display:inline-block; margin-top: 5px; width:20px; height:20px; background-color:" + cell_colors[i] + "'></div>"
|
|
355
|
+
+ "<div style='display:inline-block; vertical-align: middle; margin-bottom: 7px; margin-left:8px;'>" + data.names[i] + "</div>"
|
|
356
|
+
+ "<br/>"
|
|
357
|
+
+ "<div style='margin-left: 10px;'>"
|
|
358
|
+
// + "<div style='padding:2px; width:200px'>Strength: " + data.rounds[actRound].strength[i] + "</div>"
|
|
359
|
+
+ "<div style='padding:2px; width:200px'>Score: " + data.rounds[actRound].score[i] + "</div>"
|
|
360
|
+
+ "<div style='padding:2px; width:200px'>Alive units: " + data.rounds[actRound].alive_units[i] + "</div>"
|
|
361
|
+
+ "<div style='padding:2px; width:200px'>Strength/uni: " + str_uni + "</div>"
|
|
362
|
+
// + "<div style='padding:2px;'>Treasures: " + data.rounds[actRound].nb_treasures[i] + "</div>"
|
|
363
|
+
// + "<div style='padding:2px;'>Dwarves: " + data.rounds[actRound].alive_dwarves[i] + "</div>"
|
|
364
|
+
// + "<div style='padding:2px;'>Wizards: " + data.rounds[actRound].alive_wizards[i] + "</div>"
|
|
365
|
+
+ (data.secgame ? "<div style='padding:2px;'>CPU: " + data.rounds[actRound].cpu[i] + "</div>" : "")
|
|
366
|
+
+ "</div>"
|
|
367
|
+
+ "</span><br/><br/>";
|
|
368
|
+
}
|
|
369
|
+
$("#scores").html(scoreboard);
|
|
370
|
+
|
|
371
|
+
var order = [0, 1, 2, 3];
|
|
372
|
+
for (var i = 0; i < 3; ++i) {
|
|
373
|
+
for (var j = i + 1; j < 4; ++j) {
|
|
374
|
+
if (data.rounds[actRound].score[order[i]] < data.rounds[actRound].score[order[j]]) {
|
|
375
|
+
var k = order[i];
|
|
376
|
+
order[i] = order[j];
|
|
377
|
+
order[j] = k;
|
|
378
|
+
}
|
|
379
|
+
}
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
var totalboard = "";
|
|
383
|
+
for (var i = 0; i < data.nb_players; ++i) {
|
|
384
|
+
totalboard += "<span class='total'>"
|
|
385
|
+
+ "<div style='display:inline-block; margin-top: 5px; width:20px; height:20px; background-color:" + cell_colors[order[i]] + "'></div>"
|
|
386
|
+
+ "<div style='display:inline-block; vertical-align: middle; margin-bottom: 7px; margin-left:8px;'>"
|
|
387
|
+
+ data.rounds[actRound].score[order[i]] + "</div>"
|
|
388
|
+
+ "</span><br/><br/>";
|
|
389
|
+
}
|
|
390
|
+
$("#totals").html(totalboard);
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
|
|
394
|
+
function drawGame () {
|
|
395
|
+
console.log("Draw game");
|
|
396
|
+
// Boundary check.
|
|
397
|
+
if (actRound < 0) actRound = 0;
|
|
398
|
+
if (actRound >= data.nb_rounds) actRound = data.nb_rounds;
|
|
399
|
+
|
|
400
|
+
// Outter Rectangle.
|
|
401
|
+
context.fillStyle = grid_color;
|
|
402
|
+
context.fillRect(0, 0, tileSize*data.cols, tileSize*data.rows);
|
|
403
|
+
|
|
404
|
+
// Draw maze.
|
|
405
|
+
var meitat = tileSize/2;
|
|
406
|
+
var quart = tileSize/4;
|
|
407
|
+
var amplada = tileSize - 0.5;
|
|
408
|
+
var rows = data.rounds[actRound].rows;
|
|
409
|
+
for (var i = 0; i < data.rows; ++i) {
|
|
410
|
+
var row = rows[i];
|
|
411
|
+
for (var j = 0; j < data.cols; ++j) {
|
|
412
|
+
var cell = row[j];
|
|
413
|
+
var ii = i*tileSize;
|
|
414
|
+
var jj = j*tileSize;
|
|
415
|
+
context.fillStyle = cell_colors[cell];
|
|
416
|
+
context.fillRect(jj, ii, amplada, amplada);
|
|
417
|
+
|
|
418
|
+
// if (cell == '.'){
|
|
419
|
+
// context.fillStyle = cell_colors[cell];
|
|
420
|
+
// context.fillRect(jj, ii, amplada, amplada);
|
|
421
|
+
// }
|
|
422
|
+
// else if (cell == 'A') {
|
|
423
|
+
// var center_i = ii + meitat;
|
|
424
|
+
// var center_j = jj + meitat;
|
|
425
|
+
// var my_gradient = context.createRadialGradient(center_j, center_i, 0, center_j, center_i, meitat + 5);
|
|
426
|
+
// my_gradient.addColorStop(0, "red");
|
|
427
|
+
// my_gradient.addColorStop(1, "black");
|
|
428
|
+
// context.fillStyle = my_gradient;
|
|
429
|
+
// context.fillRect(jj, ii, tileSize, tileSize);
|
|
430
|
+
// }
|
|
431
|
+
// else if (cell == 'G') {
|
|
432
|
+
// context.strokeStyle = "#000000";
|
|
433
|
+
// context.fillStyle = "#000000";
|
|
434
|
+
// context.fillRect(jj, ii, tileSize, tileSize);
|
|
435
|
+
// var my_gradient = context.createLinearGradient(jj, ii + tileSize, jj + tileSize, ii);
|
|
436
|
+
// my_gradient.addColorStop(0, cell_colors[cell]);
|
|
437
|
+
// my_gradient.addColorStop(1, "#D0D0D0");
|
|
438
|
+
// context.fillStyle = my_gradient;
|
|
439
|
+
// context.fillRect(jj, ii, tileSize, tileSize);
|
|
440
|
+
// context.fillStyle = "#000000";
|
|
441
|
+
// context.strokeRect(jj + 0.5, ii + 0.5, tileSize - 1.5, tileSize - 1.5);
|
|
442
|
+
// }
|
|
443
|
+
// else if (cell >= 'a' && cell <= 'd') {
|
|
444
|
+
// var my_gradient = context.createLinearGradient(jj, ii, jj + tileSize, ii + tileSize);
|
|
445
|
+
// my_gradient.addColorStop(0, cell_colors[cell]);
|
|
446
|
+
// my_gradient.addColorStop(1, "#905018");
|
|
447
|
+
// context.fillStyle = my_gradient;
|
|
448
|
+
// context.fillRect(jj, ii, tileSize, tileSize);
|
|
449
|
+
// }
|
|
450
|
+
// else if (cell == 'T') {
|
|
451
|
+
// context.fillStyle = cell_colors['C'];
|
|
452
|
+
// context.fillRect(jj, ii, amplada, amplada);
|
|
453
|
+
// context.fillStyle = cell_colors[cell];
|
|
454
|
+
// var size = unitSize * tileSize * 1.2;
|
|
455
|
+
// var offset = (tileSize - size) / 2;
|
|
456
|
+
// context.beginPath();
|
|
457
|
+
// context.arc(jj + size/2 + offset, ii + size/2 + offset, size/2, 0, Math.PI*2, false);
|
|
458
|
+
// context.fill();
|
|
459
|
+
// context.stroke();
|
|
460
|
+
// }
|
|
461
|
+
// else {
|
|
462
|
+
// context.fillStyle = cell_colors[cell];
|
|
463
|
+
// context.fillRect(jj, ii, amplada, amplada);
|
|
464
|
+
// }
|
|
465
|
+
}
|
|
466
|
+
}
|
|
467
|
+
|
|
468
|
+
console.log("Start with units");
|
|
469
|
+
|
|
470
|
+
// Draw units.
|
|
471
|
+
context.lineWidth = unitLineWidth;
|
|
472
|
+
var units = data.rounds[actRound].units;
|
|
473
|
+
for (var un in units) {
|
|
474
|
+
var u = units[un];
|
|
475
|
+
if (u.player == -1) {
|
|
476
|
+
context.strokeStyle = "#000000";
|
|
477
|
+
context.fillStyle = "#FF0000";
|
|
478
|
+
}
|
|
479
|
+
else {
|
|
480
|
+
context.strokeStyle = player_colors[u.player];
|
|
481
|
+
context.fillStyle = player_colors[u.player];
|
|
482
|
+
}
|
|
483
|
+
var i = u.i;
|
|
484
|
+
var j = u.j;
|
|
485
|
+
|
|
486
|
+
if (gameAnim) {
|
|
487
|
+
if (frames >= FRAMES_PER_ROUND/2) {
|
|
488
|
+
if (u.move == 'b') i += 0.5;
|
|
489
|
+
else if (u.move == 'w') { i += 0.5; j += 0.5; }
|
|
490
|
+
else if (u.move == 'r') j += 0.5;
|
|
491
|
+
else if (u.move == 'x') { i -= 0.5; j += 0.5; }
|
|
492
|
+
else if (u.move == 't') i -= 0.5;
|
|
493
|
+
else if (u.move == 'y') { i -= 0.5; j -= 0.5; }
|
|
494
|
+
else if (u.move == 'l') j -= 0.5;
|
|
495
|
+
else if (u.move == 'z') { i += 0.5; j -= 0.5; }
|
|
496
|
+
}
|
|
497
|
+
}
|
|
498
|
+
|
|
499
|
+
if (u.type == 'a' && u.rounds == -1) drawAlive(i, j);
|
|
500
|
+
else if (u.type == 'a') drawConverting(i,j);
|
|
501
|
+
else if (u.type == 'd') drawDead(i, j);
|
|
502
|
+
else if (u.type == 'z') drawZombie(i, j);
|
|
503
|
+
}
|
|
504
|
+
|
|
505
|
+
// Draw food
|
|
506
|
+
context.strokeStyle = "#000000";
|
|
507
|
+
context.fillStyle = "#FF0000";
|
|
508
|
+
var foods = data.rounds[actRound].food;
|
|
509
|
+
for (var fo in foods){
|
|
510
|
+
var f = foods[fo];
|
|
511
|
+
var i = f.i;
|
|
512
|
+
var j = f.j;
|
|
513
|
+
drawFood(i, j);
|
|
514
|
+
}
|
|
515
|
+
}
|
|
516
|
+
|
|
517
|
+
|
|
518
|
+
// function drawBalrog (i, j, col) {
|
|
519
|
+
// context.strokeStyle = "transparent";
|
|
520
|
+
// context.fillStyle = col;
|
|
521
|
+
// context.globalAlpha = 0.5;
|
|
522
|
+
// var jm = (j-1)*tileSize;
|
|
523
|
+
// var j0 = j*tileSize;
|
|
524
|
+
// var j1 = (j+1)*tileSize;
|
|
525
|
+
// var j2 = (j+2)*tileSize;
|
|
526
|
+
// var im = (i-1)*tileSize;
|
|
527
|
+
// var i0 = i*tileSize;
|
|
528
|
+
// var i1 = (i+1)*tileSize;
|
|
529
|
+
// var i2 = (i+2)*tileSize;
|
|
530
|
+
// context.beginPath();
|
|
531
|
+
// context.moveTo(j0, im);
|
|
532
|
+
// context.lineTo(j1, im);
|
|
533
|
+
// context.lineTo(j2, i0);
|
|
534
|
+
// context.lineTo(j2, i1);
|
|
535
|
+
// context.lineTo(j1, i2);
|
|
536
|
+
// context.lineTo(j0, i2);
|
|
537
|
+
// context.lineTo(jm, i1);
|
|
538
|
+
// context.lineTo(jm, i0);
|
|
539
|
+
// context.closePath();
|
|
540
|
+
// context.fill();
|
|
541
|
+
// context.stroke();
|
|
542
|
+
// context.beginPath();
|
|
543
|
+
// context.arc(j1, i1, tileSize, 0, 0.5*Math.PI);
|
|
544
|
+
// context.fill();
|
|
545
|
+
// context.stroke();
|
|
546
|
+
// context.beginPath();
|
|
547
|
+
// context.arc(j0, i1, tileSize, 0.5*Math.PI, Math.PI);
|
|
548
|
+
// context.fill();
|
|
549
|
+
// context.stroke();
|
|
550
|
+
// context.beginPath();
|
|
551
|
+
// context.arc(j0, i0, tileSize, Math.PI, 1.5*Math.PI);
|
|
552
|
+
// context.fill();
|
|
553
|
+
// context.stroke();
|
|
554
|
+
// context.beginPath();
|
|
555
|
+
// context.arc(j1, i0, tileSize, 1.5*Math.PI, 2*Math.PI);
|
|
556
|
+
// context.fill();
|
|
557
|
+
// context.stroke();
|
|
558
|
+
// context.globalAlpha = 1.0;
|
|
559
|
+
// var size = unitSize * tileSize * 1.2;
|
|
560
|
+
// var offset = (tileSize - size) / 2;
|
|
561
|
+
// context.beginPath();
|
|
562
|
+
// context.arc(j*tileSize + size/2 + offset, i*tileSize + size/2 + offset, size/2, 0, 2*Math.PI, false);
|
|
563
|
+
// context.fill();
|
|
564
|
+
// context.stroke();
|
|
565
|
+
// }
|
|
566
|
+
|
|
567
|
+
|
|
568
|
+
function drawDead (i, j) {
|
|
569
|
+
var size = unitSize * tileSize * 0.5;
|
|
570
|
+
var offset = (tileSize - size) / 2;
|
|
571
|
+
context.beginPath();
|
|
572
|
+
context.arc(j*tileSize + size/2 + offset, i*tileSize + size/2 + offset, size/2, 0, 2*Math.PI, false);
|
|
573
|
+
context.fill();
|
|
574
|
+
context.stroke();
|
|
575
|
+
context.beginPath();
|
|
576
|
+
context.moveTo(j*tileSize + offset - 0.4*size, i*tileSize + offset - 0.4*size);
|
|
577
|
+
context.lineTo(j*tileSize + offset + 1.4*size, i*tileSize + offset + 1.4*size);
|
|
578
|
+
context.stroke();
|
|
579
|
+
context.beginPath();
|
|
580
|
+
context.moveTo(j*tileSize + offset + 1.4*size, i*tileSize + offset - 0.4*size);
|
|
581
|
+
context.lineTo(j*tileSize + offset - 0.4*size, i*tileSize + offset + 1.4*size);
|
|
582
|
+
context.stroke();
|
|
583
|
+
}
|
|
584
|
+
|
|
585
|
+
|
|
586
|
+
function drawAlive (i, j) {
|
|
587
|
+
var size = unitSize * tileSize * 0.7;
|
|
588
|
+
var offset = (tileSize - size) / 2;
|
|
589
|
+
context.beginPath();
|
|
590
|
+
context.arc(j*tileSize + size/2 + offset, i*tileSize + size/2 + offset, size/2, 0, 2*Math.PI, false);
|
|
591
|
+
context.fill();
|
|
592
|
+
context.stroke();
|
|
593
|
+
}
|
|
594
|
+
|
|
595
|
+
|
|
596
|
+
function drawFood (i, j) {
|
|
597
|
+
var size = unitSize * tileSize * 0.7;
|
|
598
|
+
var offset = (tileSize - size) / 2;
|
|
599
|
+
context.beginPath();
|
|
600
|
+
context.arc(j*tileSize + size/2 + offset, i*tileSize + size/2 + offset, size/2, 0, 2*Math.PI, false);
|
|
601
|
+
context.fill();
|
|
602
|
+
context.stroke();
|
|
603
|
+
}
|
|
604
|
+
|
|
605
|
+
function drawConverting (i, j) {
|
|
606
|
+
var inc = 3;
|
|
607
|
+
var amplada = tileSize - inc;
|
|
608
|
+
context.fillRect(j*tileSize + inc, i*tileSize + inc, amplada - inc, amplada - inc);
|
|
609
|
+
context.strokeRect(j*tileSize + inc, i*tileSize + inc, amplada - inc, amplada - inc);
|
|
610
|
+
}
|
|
611
|
+
|
|
612
|
+
|
|
613
|
+
function drawZombie (i, j) {
|
|
614
|
+
var inc = 3;
|
|
615
|
+
var amplada = tileSize - inc;
|
|
616
|
+
context.fillRect(j*tileSize + inc, i*tileSize + inc, amplada - inc, amplada - inc);
|
|
617
|
+
context.strokeRect(j*tileSize + inc, i*tileSize + inc, amplada - inc, amplada - inc);
|
|
618
|
+
}
|
|
619
|
+
|
|
620
|
+
|
|
621
|
+
// *********************************************************************
|
|
622
|
+
// Button events
|
|
623
|
+
// *********************************************************************
|
|
624
|
+
|
|
625
|
+
function playButton () {
|
|
626
|
+
gamePaused = false;
|
|
627
|
+
}
|
|
628
|
+
|
|
629
|
+
|
|
630
|
+
function pauseButton () {
|
|
631
|
+
gamePaused = true;
|
|
632
|
+
gamePreview = true; // To call render again.
|
|
633
|
+
frames = 0;
|
|
634
|
+
}
|
|
635
|
+
|
|
636
|
+
|
|
637
|
+
function startButton () {
|
|
638
|
+
gamePaused = true;
|
|
639
|
+
gamePreview = true;
|
|
640
|
+
frames = 0;
|
|
641
|
+
actRound = 0;
|
|
642
|
+
}
|
|
643
|
+
|
|
644
|
+
|
|
645
|
+
function endButton () {
|
|
646
|
+
gamePreview = true;
|
|
647
|
+
frames = 0;
|
|
648
|
+
actRound = data.nb_rounds;
|
|
649
|
+
}
|
|
650
|
+
|
|
651
|
+
|
|
652
|
+
function animButton () {
|
|
653
|
+
gameAnim = !gameAnim;
|
|
654
|
+
}
|
|
655
|
+
|
|
656
|
+
|
|
657
|
+
function closeButton () {
|
|
658
|
+
window.close();
|
|
659
|
+
}
|
|
660
|
+
|
|
661
|
+
|
|
662
|
+
// *********************************************************************
|
|
663
|
+
// Keyboard and Mouse events
|
|
664
|
+
// *********************************************************************
|
|
665
|
+
|
|
666
|
+
function onDocumentMouseWheel (event) {
|
|
667
|
+
}
|
|
668
|
+
|
|
669
|
+
|
|
670
|
+
function onDocumentKeyDown (event) {
|
|
671
|
+
}
|
|
672
|
+
|
|
673
|
+
|
|
674
|
+
function onDocumentKeyUp (event) {
|
|
675
|
+
// http://www.webonweboff.com/tips/js/event_key_codes.aspx
|
|
676
|
+
switch (event.keyCode) {
|
|
677
|
+
case 36: // Start.
|
|
678
|
+
gamePreview = true;
|
|
679
|
+
actRound = 0;
|
|
680
|
+
frames = 0;
|
|
681
|
+
break;
|
|
682
|
+
|
|
683
|
+
case 35: // End.
|
|
684
|
+
gamePreview = true;
|
|
685
|
+
actRound = data.nb_rounds;
|
|
686
|
+
frames = 0;
|
|
687
|
+
break;
|
|
688
|
+
|
|
689
|
+
case 33: // PageDown.
|
|
690
|
+
gamePreview = true;
|
|
691
|
+
actRound -= 10;
|
|
692
|
+
frames = 0;
|
|
693
|
+
break;
|
|
694
|
+
|
|
695
|
+
case 34: // PageUp.
|
|
696
|
+
gamePreview = true;
|
|
697
|
+
actRound += 10;
|
|
698
|
+
frames = 0;
|
|
699
|
+
break;
|
|
700
|
+
|
|
701
|
+
case 38: // ArrowUp.
|
|
702
|
+
case 37: // ArrowLeft.
|
|
703
|
+
gamePaused= true;
|
|
704
|
+
gamePreview = true;
|
|
705
|
+
--actRound;
|
|
706
|
+
frames = 0;
|
|
707
|
+
break;
|
|
708
|
+
|
|
709
|
+
case 40: // ArrowDown.
|
|
710
|
+
case 39: // ArrowRight.
|
|
711
|
+
gamePaused = true;
|
|
712
|
+
gamePreview = true;
|
|
713
|
+
++actRound;
|
|
714
|
+
frames = 0;
|
|
715
|
+
break;
|
|
716
|
+
|
|
717
|
+
case 32: // Space.
|
|
718
|
+
if (gamePaused) playButton();
|
|
719
|
+
else pauseButton();
|
|
720
|
+
break;
|
|
721
|
+
|
|
722
|
+
case 72: // "h"
|
|
723
|
+
help();
|
|
724
|
+
break;
|
|
725
|
+
|
|
726
|
+
default:
|
|
727
|
+
// $("#debug").html(event.keyCode);
|
|
728
|
+
break;
|
|
729
|
+
}
|
|
730
|
+
}
|
|
731
|
+
|
|
732
|
+
|
|
733
|
+
function onWindowResize (event) {
|
|
734
|
+
// Constants.
|
|
735
|
+
var header_height = 150;
|
|
736
|
+
var canvas_margin = 20;
|
|
737
|
+
|
|
738
|
+
// Set canvas size.
|
|
739
|
+
var size = Math.min(document.body.offsetWidth, document.body.offsetHeight - header_height) - canvas_margin*2;
|
|
740
|
+
|
|
741
|
+
canvas.width = size;
|
|
742
|
+
canvas.height = size;
|
|
743
|
+
|
|
744
|
+
var max_dimension = Math.max(data.cols,data.rows);
|
|
745
|
+
tileSize = size / max_dimension;
|
|
746
|
+
|
|
747
|
+
drawGame();
|
|
748
|
+
}
|
|
749
|
+
|
|
750
|
+
|
|
751
|
+
function help () {
|
|
752
|
+
// Opens a new popup with the help page.
|
|
753
|
+
var win = window.open('help.html', 'name', 'height=400, width=300');
|
|
754
|
+
if (window.focus) win.focus();
|
|
755
|
+
return false;
|
|
756
|
+
}
|
|
757
|
+
|
|
758
|
+
|
|
759
|
+
// *********************************************************************
|
|
760
|
+
// This function is called periodically.
|
|
761
|
+
// *********************************************************************
|
|
762
|
+
|
|
763
|
+
function mainloop () {
|
|
764
|
+
// Configure buttons.
|
|
765
|
+
if (gamePaused) {
|
|
766
|
+
$("#but_play").show();
|
|
767
|
+
$("#but_pause").hide();
|
|
768
|
+
}
|
|
769
|
+
else {
|
|
770
|
+
$("#but_play").hide();
|
|
771
|
+
$("#but_pause").show();
|
|
772
|
+
}
|
|
773
|
+
|
|
774
|
+
if (actRound < 0) actRound = 0;
|
|
775
|
+
|
|
776
|
+
if (actRound > data.nb_rounds) {
|
|
777
|
+
actRound = data.nb_rounds;
|
|
778
|
+
gamePaused = true;
|
|
779
|
+
frames = 0;
|
|
780
|
+
}
|
|
781
|
+
|
|
782
|
+
if (!gamePaused || gamePreview) {
|
|
783
|
+
updateGame();
|
|
784
|
+
drawGame();
|
|
785
|
+
writeGameState();
|
|
786
|
+
|
|
787
|
+
if (gamePreview) {
|
|
788
|
+
frames = 0;
|
|
789
|
+
gamePreview = false;
|
|
790
|
+
}
|
|
791
|
+
else {
|
|
792
|
+
++frames;
|
|
793
|
+
if (frames == FRAMES_PER_ROUND) {
|
|
794
|
+
frames = 0;
|
|
795
|
+
actRound += gameDirection;
|
|
796
|
+
}
|
|
797
|
+
}
|
|
798
|
+
}
|
|
799
|
+
|
|
800
|
+
// Periodically call mainloop.
|
|
801
|
+
var frame_time = 1000/speed;
|
|
802
|
+
setTimeout(mainloop, frame_time);
|
|
803
|
+
}
|
|
804
|
+
|
|
805
|
+
|
|
806
|
+
// *********************************************************************
|
|
807
|
+
// Main function, it is called when the document is ready.
|
|
808
|
+
// *********************************************************************
|
|
809
|
+
|
|
810
|
+
function init () {
|
|
811
|
+
// Get url parameters.
|
|
812
|
+
var game;
|
|
813
|
+
if (getURLParameter("sub") != null) {
|
|
814
|
+
var domain = window.location.protocol + "//" + window.location.host;
|
|
815
|
+
if (getURLParameter("nbr") != null)
|
|
816
|
+
game = domain + "/?cmd=lliuraments&sub=" + getURLParameter("sub") + "&nbr=" + getURLParameter("nbr") + "&download=partida";
|
|
817
|
+
else
|
|
818
|
+
game = domain + "/?cmd=partida&sub=" + getURLParameter("sub") + "&download=partida";
|
|
819
|
+
}
|
|
820
|
+
else game = getURLParameter("game");
|
|
821
|
+
|
|
822
|
+
if (game == null || game == "") {
|
|
823
|
+
// Ask the user for a game input.
|
|
824
|
+
var inputdiv = document.getElementById('inputdiv')
|
|
825
|
+
inputdiv.style.display = "";
|
|
826
|
+
document.getElementById('file').addEventListener('change', function(evt) {
|
|
827
|
+
//http://www.html5rocks.com/en/tutorials/file/dndfiles/
|
|
828
|
+
var file = evt.target.files[0];
|
|
829
|
+
var reader = new FileReader();
|
|
830
|
+
reader.readAsText(file);
|
|
831
|
+
reader.onloadend = function(evt) {
|
|
832
|
+
if (evt.target.readyState != FileReader.DONE) alert("Error accessing file.");
|
|
833
|
+
else { // DONE == 2.
|
|
834
|
+
inputdiv.style.display = "none";
|
|
835
|
+
document.getElementById("loadingdiv").style.display = "";
|
|
836
|
+
initGame(reader.result);
|
|
837
|
+
}
|
|
838
|
+
};
|
|
839
|
+
}, false);
|
|
840
|
+
}
|
|
841
|
+
else {
|
|
842
|
+
document.getElementById("loadingdiv").style.display = "";
|
|
843
|
+
// Load the given game.
|
|
844
|
+
loadFile(game, initGame);
|
|
845
|
+
}
|
|
846
|
+
}
|