-
Notifications
You must be signed in to change notification settings - Fork 24
/
global.js
executable file
·376 lines (286 loc) · 9.21 KB
/
global.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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
// oft-referenced constants, and a few simple methods.
const searchParams = new URLSearchParams(window.location.search);
const DEFAULT_FUNDS = searchParams.get('FUNDS') ? 999 : 32;
const winloc = window.location.href.toString();
const ua = navigator.userAgent;
const FPS = 30;
const FRAMERATE = 1000 / FPS;
/**
* Skip frame(s) as needed, prevent the game from running too fast.
*
* Note: 144 hz monitors (maybe on Windows?) may need 0.33 or less,
* or else the loop may run at 35+ FPS. This may be a 144 hz
* monitor thing, not necessarily tied to Windows. TBD.
*
* It may be this value needs to be made dynamic, starting at 0.5
* and trickling down to 0.33 or so if the frame rate is > 30FPS.
*
*/
const frameOffset = parseFloat(searchParams.get('frameOffset')) || 0.33;
const FRAME_MIN_TIME = (1000 / 60) * (60 / FPS) - (1000 / 60) * frameOffset;
const unlimitedFrameRate = searchParams.get('frameRate=*');
/**
* Evil tricks needed because Safari 6 (and Webkit nightly)
* scale text after rasterization - thus, there's an option
* to use document[element].style.zoom vs. transform: scale3d()
* which renders text cleanly. Both have minor quirks.
* force-enable transform under Safari 6 w/ #forceTransform=1
*/
const isWebkit = ua.match(/webkit/i);
const isChrome = !!(isWebkit && (ua.match(/chrome/i) || []).length);
const isFirefox = !!ua.match(/firefox/i);
const isSafari = (isWebkit && !isChrome && !!ua.match(/safari/i));
// iOS devices if they report as such, e.g., iPad when "request mobile website" is selected (vs. desktop) - OR, if "touch support" exists(?)
const isMobile = !!ua.match(/mobile|iphone|ipad/i) || navigator?.maxTouchPoints > 0;
const isiPhone = !!ua.match(/iphone/i);
// bare-bones "request mobile website" iOS detection
const isMobileIOS = !!ua.match(/iphone|ipad/i);
// whether off-screen elements are forcefully removed from the DOM.
// may be expensive up front, and/or cause style recalcs while
// scrolling the world. the fastest nodes are the ones that aren't there.
const useDOMPruning = !searchParams.get('noDomPruning');
const debug = searchParams.get('debug');
// TODO: get rid of this.
const debugType = searchParams.get('debugType');
const debugCollision = searchParams.get('debugCollision');
const DEFAULT_VOLUME = 25;
const rad2Deg = 180 / Math.PI;
// used for various measurements in the game
const worldWidth = 8192;
const worldHeight = 380;
const forceZoom = !!(searchParams.get('forceZoom'));
const forceTransform = !!(searchParams.get('forceTransform'));
let tutorialMode = !!(searchParams.get('tutorial'));
function setTutorialMode(state) {
tutorialMode = state;
}
// classic missile style
const defaultMissileMode = 'default-missile-mode';
// can also be enabled by pressing "C".
const rubberChickenMode = 'rubber-chicken-mode';
// can also be enabled by pressing "B".
const bananaMode = 'banana-mode';
// methods which prefer brevity, vs. being tacked onto `common` or `utils`
/**
* Type table, supporting both camelCase and dash-type lookups
* e.g., { parachuteInfantry : 'parachute-infantry' }
* and { 'parachute-infantry': 'parachute-infantry' }
* Dash-case is used mostly for DOM / CSS, camelCase for JS
*/
const TYPES = (() => {
// assign 1:1 key / value strings in a DRY fashion
const types = 'base, bomb, balloon, bunker, chain, cloud, cornholio, engineer, flame, gunfire, helicopter, infantry, end-bunker, landing-pad, missile-launcher, parachute-infantry, smart-missile, smoke, shrapnel, super-bunker, tank, turret, terrain-item, van';
const result = {};
types.split(', ').forEach((type) => {
// { bunker: 'bunker' }
result[type] = type;
// dash-case to camelCase
if (type.indexOf('-') !== -1) {
// missile-launcher -> ['missile', 'launcher']
const a = type.split('-');
// launcher -> Launcher
a[1] = a[1].charAt(0).toUpperCase() + a[1].slice(1);
// { missileLauncher: 'missile-launcher' }
result[a.join('')] = type;
}
});
return result;
})();
const PRETTY_TYPES = {
[TYPES.tank]: 'Tank',
[TYPES.missileLauncher]: 'Missile Launcher',
[TYPES.van]: 'Van',
[TYPES.infantry]: 'Infantry',
[TYPES.engineer]: 'Engineer'
}
// set, and updated as applicable via network
let defaultSeeds = [];
for (let i = 0; i < 8; i++) {
defaultSeeds.push(Math.floor(Math.random() * 0xFFFFFFFF));
}
let defaultSeed = defaultSeeds[0];
let seed = Math.floor(defaultSeed);
let seedsByType = {};
function setSeedsByType() {
// TYPES include camelCase entries e.g., missileLauncher, those will be ignored here.
for (let type in TYPES) {
if (!type.match(/[A-Z]/)) {
seedsByType[type] = Math.floor(defaultSeed);
}
}
}
// start with the default, until (and if) updated via network.
setSeedsByType();
function setDefaultSeed(newDefaultSeed, newDefaultSeeds) {
defaultSeed = newDefaultSeed;
defaultSeeds = newDefaultSeeds;
seed = Math.floor(defaultSeed);
setSeedsByType();
}
// rng: random number *generator*. Tweaked to allow usage of a range of seeds.
// hat tip: https://github.com/mitxela/webrtc-pong/blob/master/pong.htm#L176
function rng(number = 1, type, seedOffset) {
let t;
if (type && seedsByType[type]) {
t = seedsByType[type] += 0x6D2B79F5;
} else if (seedOffset >= 0 && defaultSeeds[seedOffset]) {
t = defaultSeeds[seedOffset] += 0x6D2B79F5;
} else {
t = seed += 0x6D2B79F5;
}
t = Math.imul(t ^ t >>> 15, t | 1);
t ^= t + Math.imul(t ^ t >>> 7, t | 61);
return number * (((t ^ t >>> 14) >>> 0) / 4294967296);
}
function rnd(number) {
return Math.random() * number;
}
function rngInt(number, type) {
return parseInt(rng(number, type), 10);
}
function rndInt(number) {
return parseInt(rnd(number), 10);
}
function rngPlusMinus(number = 1, type) {
return rng(number, type) >= 0.5 ? number : -number;
}
function plusMinus(number = 1) {
return Math.random() >= 0.5 ? number : -number;
}
function oneOf(array) {
if (!array?.length) return;
return array[rndInt(array.length)];
}
function getTypes(typeString, options = { group: 'enemy', exports: null }) {
/**
* Used for collision and nearby checks, e.g., ground units that tanks look out for
* typeString: String to array, e.g., 'tank, van, infantry' mapped to TYPES
* options object: group = all, friendly, or enemy - reducing # of objects to check.
*/
if (!typeString?.split) return [];
let { exports, group } = options;
// if exports but no group, assume enemy.
if (!group) {
group = 'enemy';
}
// if NOT looking for all, determine the appropriate group.
if (group !== 'all') {
group = determineGroup(group, exports);
}
// normalize delimiters, get array.
return parseTypeString(typeString).map((item) => {
// "tank:friendly", per-type override
if (item.indexOf(':') !== -1) {
const typeAndGroup = item.split(':');
return {
type: TYPES[typeAndGroup[0]],
group: determineGroup(typeAndGroup[1], exports)
};
}
// just "tank", use function signature group
return { type: TYPES[item], group };
});
}
function determineGroup(group = 'all', exports) {
// if the default, no additional work required.
if (group === 'all') return group;
if (!exports) {
console.warn(`determineGroup(${group}): missing exports required to determine target`, arguments);
return;
}
if (exports.data.isEnemy || exports.data.hostile) {
// "bad guy" - whatever they're looking for, maps to the opposite array in-game.
// e.g., enemy tank seeking an enemy = lookups in "friendly" game object array.
group = enemyGroupMap[group];
}
return group;
}
function parseTypeString(typeString) {
// helper method
if (!typeString?.replace) return [];
// 'tank, van, infantry' -> ['tank', 'van', 'infantry']
return typeString.replace(/[\s|,]+/g, ' ').split(' ');
}
// normalize delimiters -> array; no "group" handling, here.
const parseTypes = (typeString) => parseTypeString(typeString).map((item) => TYPES[item]);
const enemyGroupMap = {
/**
* The game stores enemy objects in enemy arrays, and friendly -> friendly.
* Ergo, when enemies are looking for friendly, they get the enemy array
* and vice-versa. This is due to legacy names, and could be improved.
*/
friendly: 'enemy',
enemy: 'friendly'
};
const COSTS = {
[TYPES.missileLauncher]: {
funds: 3,
count: 1,
css: 'can-not-order-missile-launcher'
},
[TYPES.tank]: {
funds: 4,
count: 1,
css: 'can-not-order-tank'
},
[TYPES.van]: {
funds: 2,
count: 1,
css: 'can-not-order-van',
},
[TYPES.infantry]: {
funds: 5,
count: 5,
css: 'can-not-order-infantry',
},
[TYPES.engineer]: {
funds: 5,
count: 2,
css: 'can-not-order-engineer'
}
};
export {
DEFAULT_FUNDS,
TYPES,
PRETTY_TYPES,
COSTS,
winloc,
FRAME_MIN_TIME,
FPS,
FRAMERATE,
unlimitedFrameRate,
defaultSeed,
defaultSeeds,
getTypes,
parseTypes,
isWebkit,
isChrome,
isFirefox,
isSafari,
isMobile,
isiPhone,
isMobileIOS,
useDOMPruning,
debug,
debugCollision,
debugType,
DEFAULT_VOLUME,
rad2Deg,
worldWidth,
worldHeight,
forceZoom,
forceTransform,
tutorialMode,
defaultMissileMode,
rubberChickenMode,
bananaMode,
oneOf,
rnd,
rng,
rndInt,
rngInt,
plusMinus,
rngPlusMinus,
setDefaultSeed,
setTutorialMode
};