This repository has been archived by the owner on Dec 7, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathworldloader.js
177 lines (145 loc) · 5.01 KB
/
worldloader.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
'use strict';
function ZZTWorld() {}
function ZZTWorldLoader()
{
}
ZZTWorldLoader.prototype.init = function(url, callback)
{
var self = this;
var request = new XMLHttpRequest();
request.open("GET", url, true);
request.responseType = 'arraybuffer';
request.onload = function(e)
{
var world = null;
if (this.status == 200)
{
var stream = new ZZTFileStream(this.response);
world = self.parseWorldData(stream);
}
callback(world);
}
request.send();
}
ZZTWorldLoader.prototype.parseWorldData = function(stream)
{
var world = new ZZTWorld();
world.worldType = stream.getInt16();
world.numBoards = stream.getInt16();
world.playerAmmo = stream.getInt16();
world.playerGems = stream.getInt16();
world.playerKeys = new Array(7);
for (var i = 0; i < 7; ++i)
world.playerKeys[i] = stream.getBoolean();
world.playerHealth = stream.getInt16();
world.playerBoard = stream.getInt16();
world.playerTorches = stream.getInt16();
world.torchCycles = stream.getInt16();
world.energyCycles = stream.getInt16();
stream.position += 2; /* unused */
world.playerScore = stream.getInt16();
world.worldName = stream.getFixedPascalString(20);
world.flag = new Array(10);
for (var i = 0; i < 10; ++i)
world.flag[i] = stream.getFixedPascalString(20);
world.timeLeft = stream.getInt16();
stream.position += 2; /* playerdata pointer */
world.locked = stream.getBoolean();
world.board = [];
/* board information then starts at offset 512 */
stream.position = 512;
for (var i = 0; i < world.numBoards; ++i)
world.board.push(this.parseZZTBoard(stream));
return world;
}
ZZTWorldLoader.prototype.parseZZTBoard = function(stream)
{
var boardOffset = stream.position;
var boardSize = stream.getInt16();
var board = new ZZTBoard;
board.name = stream.getFixedPascalString(50);
board.width = 60;
board.height = 25;
board.player = null;
var tiles = [];
/* what follows now is RLE data, encoding 1500 tiles */
while (tiles.length < (board.width * board.height))
{
var count = stream.getUint8();
var typeid = stream.getUint8();
var color = stream.getUint8();
/* A count of zero actually means 256 tiles. The built-in editor
never encodes like this, but some other editors do. */
if (count == 0) count = 256;
for (var i = 0; i < count; ++i)
{
tiles.push(makeTile(typeid, color));
}
}
board.tiles = tiles;
/* following the RLE data, we then have... */
board.maxPlayerShots = stream.getUint8();
board.isDark = stream.getUint8();
board.exitNorth = stream.getUint8();
board.exitSouth = stream.getUint8();
board.exitWest = stream.getUint8();
board.exitEast = stream.getUint8();
board.restartOnZap = stream.getUint8();
board.onScreenMessage = stream.getFixedPascalString(58); /* never used? */
board.messageTimer = 0;
board.playerEnterX = stream.getUint8();
board.playerEnterY = stream.getUint8();
board.timeLimit = stream.getInt16();
stream.position += 16; /* unused */
var statusElementCount = stream.getInt16() + 1;
var statusElement = [];
for (var i = 0; i < statusElementCount; ++i)
statusElement.push(this.parseStatusElement(stream));
/* for objects with code pointers referring to a different object, link them. */
for (var i = 0; i < statusElementCount; ++i)
{
if (statusElement[i].codeLength < 0)
statusElement[i].code = this.statusElement[-this.statusElement[i].codeLength].code;
}
board.statusElement = statusElement;
/* update all the line characters */
board.updateLines();
/* jump to next board */
stream.position = boardOffset + boardSize + 2;
return board;
}
ZZTWorldLoader.prototype.parseStatusElement = function(stream)
{
var status = {};
/* x and y coordinates are 1-based for some reason */
status.x = stream.getUint8() - 1;
status.y = stream.getUint8() - 1;
status.xStep = stream.getInt16();
status.yStep = stream.getInt16();
status.cycle = stream.getInt16();
status.param1 = stream.getUint8();
status.param2 = stream.getUint8();
status.param3 = stream.getUint8();
status.follower = stream.getInt16();
status.leader = stream.getInt16();
var underType = stream.getUint8();
var underColor = stream.getUint8();
status.underTile = makeTile(underType, underColor);
stream.position += 4; /* pointer is not used when loading */
status.currentInstruction = stream.getInt16();
status.codeLength = stream.getInt16();
/* for ZZT and not Super ZZT, eight bytes of padding follow */
stream.position += 8;
/* if status.codeLength is positive, there is that much ZZT-OOP code following */
if (status.codeLength > 0)
{
status.code = stream.getFixedString(status.codeLength);
}
else
{
/* it's negative, which means that we'll need to look at a different
object in order to use it's code instead; we'll do that later. */
status.code = null;
}
return status;
}