-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathscript.js
563 lines (538 loc) · 20.9 KB
/
script.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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
(function() {
var teams = ['Hoboken Zephyrs', 'Shelbyville Sharks', 'Santa Destroy Warriors', 'New New York Yankees', 'North Shore High Lions'];
var sports = ['Airball', 'Blernsball', 'Brockian Ultra-Cricket', 'Chess', 'Dungeons & Dungeons', 'Laserball', 'Quidditch'];
var residentMultiplier = 1.05;
var commerceMultiplier = 1.1;
var industryMultiplier = 1.1;
var format = function(amount, symbol) {
if (amount > 9) {
amount = Math.floor(amount);
} else {
amount = Math.floor(amount * 100) / 100;
}
if (amount > 9999999) {
amount = amount.toExponential(2);
}
return amount + (symbol || '¤');
};
var formatTime = function(city, cost) {
var time = Math.max(0, Math.ceil((cost - city.currency) / city.tax));
return format(time, time == 1 ? ' second' : ' seconds');
}
var output = function(element, attributes, head) {
var html = [];
if (head) {
html.push('<b>' + head + '</b>');
}
for (var k in attributes) {
html.push(k + ': <b>' + attributes[k] + '</b>');
}
element.innerHTML = html.join('<br>');
};
var Zone = function(city, type, symbol, load) {
load = load || {};
this.sizes = [];
var delta = 10;
for (var i = 1; i <= 10; i++) {
var label = symbol;
if (i <= 3) {
for (var j = 0; j < i - 1; j++) {
label += symbol;
}
} else {
label += 'x' + i;
}
this.sizes.push({
amount: 0,
built: 0,
density: Math.pow(10, .5 * i * i + .5 * i),
label: label
});
}
this.city = city;
this.type = type;
this.symbol = symbol;
this.tax = load.tax || 10;
this.demand = 0;
var container = document.createElement('div');
container.className = 'panel ' + type.toLowerCase();
container.innerHTML = '<h1>' + type + '</h1>';
for (var i = 0; i < this.sizes.length; i++) {
var data = this.sizes[i];
if (load[i]) {
data.amount = load[i].amount || 0;
data.built = load[i].built || 0;
}
data.button = document.createElement('button');
data.button.innerHTML = 'Zone ' + data.label;
data.button.addEventListener('click', this.buy.bind(this, i));
data.button.style.display = 'none';
data.alt = document.createElement('div');
data.alt.className = 'alt';
data.status = document.createElement('div');
data.status.style.display = 'none';
container.appendChild(data.status);
container.appendChild(data.button);
container.appendChild(data.alt);
}
this.city.container.appendChild(container);
this.city.items.push(this);
};
Zone.prototype.buy = function(size) {
if (this.city.spend(this.price(size))) {
this.sizes[size].amount++;
this.city.update();
}
};
Zone.prototype.capacity = function() {
var total = 0;
for (var i = 0; i < this.sizes.length; i++) {
total += this.sizes[i].built * this.sizes[i].density;
}
return total;
};
Zone.prototype.data = function() {
var serial = {tax: this.tax}
for (var i = 0; i < this.sizes.length; i++) {
serial[i] = {};
serial[i].amount = this.sizes[i].amount;
serial[i].built = this.sizes[i].built;
}
return serial;
};
Zone.prototype.income = function(tax) {
return (tax || this.tax) / 100 * this.capacity() / 10;
}
Zone.prototype.price = function(size) {
return Math.floor(this.sizes[size].density *
Math.pow(1.1, this.sizes[size].amount));
};
Zone.prototype.update = function() {
var visible = true;
var demand = this.demand;
for (var i = this.sizes.length - 1; i >= 0; i--) {
var data = this.sizes[i];
demand -= data.built * data.density;
}
for (var i = 0; i < this.sizes.length; i++) {
var data = this.sizes[i];
var cost = this.price(i);
output(data.status, {
Price: format(cost),
Built: data.built + ' / ' + data.amount,
});
output(data.alt, {
'Zone capacity': format(data.density, this.symbol),
'Current demand': format(this.demand, this.symbol),
'Total built': format(this.capacity(), this.symbol),
'Total zoned': format(this.zoned(), this.symbol),
'Type income': format(this.income()),
'Time to purchase': formatTime(this.city, cost),
}, 'Add ' + this.type + ' Zones');
data.button.disabled = cost > this.city.currency;
if (demand > 0 && data.built < data.amount) {
data.built++;
demand -= data.density;
}
data.button.style.display = visible ? '' : 'none';
data.status.style.display = visible ? '' : 'none';
visible = !!data.amount;
}
};
Zone.prototype.zoned = function() {
var total = 0;
for (var i = 0; i < this.sizes.length; i++) {
total += this.sizes[i].amount * this.sizes[i].density;
}
return total;
};
var Update = function(city, level, cost, scale, message, levels, stats) {
this.city = city;
this.level = level || 0;
this.cost = cost;
this.scale = scale || 2;
this.message = message;
this.levels = levels || [];
this.stats = stats;
this.container = document.createElement('div');
this.container.className = 'update';
this.button = document.createElement('button');
this.button.addEventListener('click', this.buy.bind(this));
this.container.appendChild(this.button);
this.alt = document.createElement('div');
this.alt.className = 'alt';
this.container.appendChild(this.alt);
this.label = document.createElement('label');
this.container.appendChild(this.label);
this.city.updates.appendChild(this.container);
this.city.items.push(this);
};
Update.prototype.buy = function() {
if (this.city.spend(this.price())) {
this.level++;
this.city.update();
}
};
Update.prototype.price = function() {
return this.cost * Math.pow(this.scale, this.level);
};
Update.prototype.update = function() {
var message = this.levels[this.level] || this.message;
if (!message) {
this.container.style.display = 'none';
return;
} else if (typeof(message) == 'function') {
message = message(this);
}
var cost = this.price();
if (this.stats) {
this.stats();
} else {
output(this.alt, {
'Time to purchase': formatTime(this.city, cost),
}, this.title);
}
this.button.innerHTML = format(cost);
this.button.disabled = cost > this.city.currency;
this.label.innerHTML = message.replace('CITY', '<b>' + this.city.name + '</b>');
};
var City = function(data) {
this.currency = data.currency || 100;
this.day = data.day || 0;
this.name = data.name || 'The City';
this.population = data.population || 0;
this.tax = 0;
this.container = document.createElement('div');
this.container.className = 'city';
this.status = document.createElement('div');
this.status.className = 'status';
this.container.appendChild(this.status);
this.items = [];
this.resident = new Zone(this, 'Residential', '♥', data.resident);
this.commerce = new Zone(this, 'Commercial', '♦', data.commerce);
this.industry = new Zone(this, 'Industrial', '♣', data.industry);
this.updates = document.createElement('div');
this.container.appendChild(this.updates);
this.transport = new Update(this, data.transport, 1000, 2.1,
'Increase advertising budget.', [
'Add a road to CITY.',
'Connect a highway to CITY.',
'Connect the railway to CITY.',
'Build a seaport in CITY.',
'Build an airport in CITY.',
'Advertise CITY in other cities.',
'Absorb neighboring city into CITY.',
'Advertise CITY in other countries.',
'Build a spaceport in CITY.',
'Build cloning vats in CITY.',
'Build a space elevator in CITY.',
'Attach CITY to teleporter network.',
'Advertise CITY on other planets.',
'Advertise CITY in other solar systems.',
'Advertise CITY to other galaxies.',
], function() {
var multiplier = Math.max(1.01, 2 - this.city.resident.tax / 100);
output(this.alt, {
Multiplier: format(multiplier, '×'),
'Current rate': format(Math.pow(multiplier, this.level), '♥ per second'),
'Upgrade rate': format(Math.pow(multiplier, this.level + 1), '♥ per second'),
'Time to purchase': formatTime(this.city, this.price()),
}, 'Gain population faster');
});
this.residentDemand = new Update(this, data.residentDemand, 5000, 1.9,
'Pay people for moving to CITY.', [
'Build a school in CITY.',
'Build a hospital in CITY.',
'Increase CITY education budget.',
'Build a college in CITY.',
'Increase CITY healthcare budget.',
'Add parks to CITY.',
'Build a marina in CITY.',
'Build a zoo in CITY.',
'Add wildlife to CITY parks.',
'Build a sports stadium in CITY.',
'Build a pleasure dome in CITY.',
'Build a world wonder in CITY.',
'Embue CITY park wildlife with magic.',
'Provide free healthcare in CITY.',
'Move other world wonders to CITY.',
'Build Fountain of Youth in CITY.',
], function() {
output(this.alt, {
Multiplier: format(residentMultiplier, '×'),
'Current demand': format(this.city.resident.demand, '♥'),
'Upgrade demand': format(this.city.resident.demand * residentMultiplier, '♥'),
'Time to purchase': formatTime(this.city, this.price()),
}, 'Increase residential demand');
});
this.commerceDemand = new Update(this, data.commerceDemand, 5000, 1.8,
'Give corporations extra votes.', [
'Put up billboards in CITY.',
'Build a mall in CITY.',
'Build direct CITY monorail to mall.',
'Legalize gambling in CITY.',
'Upgrade CITY Mall to CITY Megamall.',
'Add giant TVs to all CITY intersections.',
'Remove liquor restrictions.',
'Allow anonymously held corporations.',
'Legalize prositution in CITY.',
'Turn CITY into mall.',
'Declare corporations people.',
'Declare corporations better people.',
'Legalize all narcotics.',
'Give corporations CITY keys.',
], function() {
output(this.alt, {
Multiplier: format(commerceMultiplier, '×'),
'Current demand': format(this.city.commerce.demand, '♦'),
'Upgrade demand': format(this.city.commerce.demand * commerceMultiplier, '♦'),
'Time to purchase': formatTime(this.city, this.price()),
}, 'Increase commercial demand');
});
this.industryDemand = new Update(this, data.industryDemand, 10000, 1.7,
'Increase robot workforce.', [
'Build a power plant in CITY.',
'Build a factory in CITY.',
'Build an army base in CITY.',
'Repeal environmental protections.',
'Build a mine in CITY.',
'Automate CITY construction.',
'Upgrade CITY Power Plant to fission.',
'Build worker housing near CITY.',
'Upgrade CITY mine to strip mine.',
'Build toxic waste dump in CITY.',
'Upgrade CITY Power Plant to fusion.',
'Remove minimum wage in CITY.',
'Build CITY robot factories.',
'Upgrade workers to cyborgs.',
'Repeal human rights protections.',
'Nerve staple cyborg workers.',
], function() {
output(this.alt, {
Multiplier: format(industryMultiplier, '×'),
'Current demand': format(this.city.industry.demand, '♣'),
'Upgrade demand': format(this.city.industry.demand * industryMultiplier, '♣'),
'Time to purchase': formatTime(this.city, this.price()),
}, 'Increase industrial demand');
});
this.residentTax = new Update(this, data.residentTax, 1000, 2.6, function() {
return 'Raise Residential tax (<b>' + this.resident.tax + '</b>%).'}.bind(this), [],
function() {
output(this.alt, {
'New Rate': this.city.resident.tax + 1 + '%',
'Current income': format(this.city.resident.income()),
'Upgrade income': format(this.city.resident.income(this.city.resident.tax + 1)),
'Time to purchase': formatTime(this.city, this.price()),
}, 'Raise residential taxes');
});
this.commerceTax = new Update(this, data.commerceTax, 1000, 2.4, function() {
return 'Raise Commercial tax (<b>' + this.commerce.tax + '</b>%).'}.bind(this), [],
function() {
output(this.alt, {
'New Rate': this.city.commerce.tax + 1 + '%',
'Current income': format(this.city.commerce.income()),
'Upgrade income': format(this.city.commerce.income(this.city.commerce.tax + 1)),
'Time to purchase': formatTime(this.city, this.price()),
}, 'Raise commercial taxes');
});
this.industryTax = new Update(this, data.industryTax, 1000, 2.2, function() {
return 'Raise Industrial tax (<b>' + this.industry.tax + '</b>%).'}.bind(this), [],
function() {
output(this.alt, {
'New Rate': this.city.industry.tax + 1 + '%',
'Current income': format(this.city.industry.income()),
'Upgrade income': format(this.city.industry.income(this.city.industry.tax + 1)),
'Time to purchase': formatTime(this.city, this.price()),
}, 'Raise industrial taxes');
});
this.rename = new Update(this, data.rename, 1000, 1.2, 'Rename CITY.');
this.rename.button.addEventListener('click', function(){
this.name = prompt('Rename ' + this.name + ' to:');
this.update();
}.bind(this));
this.reset = new Update(this, 0, 0, 1, 'Reset game.');
this.reset.button.addEventListener('click', function(){
if (confirm('Are you sure you want to reset the game?')) {
localStorage.setItem('cityclicker', '');
document.body.removeChild(this.container);
city = new City({});
}
}.bind(this));
this.news = new Update(this, data.news, 1, 1.07, function() {
if (this.population > 1e16) {
return 'Receive CITY thought broadcast.';
} else if (this.population > 1e12) {
return 'Check CITY app.';
} else if (this.population > 1e8) {
return 'Watch CITY news.';
} else if (this.population > 1e4) {
return 'Check CITY website.';
} else {
return 'Read CITY Times.';
}
}.bind(this));
this.news.button.addEventListener('click', this.report.bind(this));
this.newspaper = document.createElement('div');
this.newspaper.className = 'newspaper';
this.container.appendChild(this.newspaper);
this.update();
document.body.appendChild(this.container);
};
City.prototype.data = function() {
return {
currency: this.currency,
day: this.day,
name: this.name,
population: this.population,
resident: this.resident.data(),
commerce: this.commerce.data(),
industry: this.industry.data(),
transport: this.transport.level,
residentDemand: this.residentDemand.level,
commerceDemand: this.commerceDemand.level,
industryDemand: this.industryDemand.level,
residentTax: this.residentTax.level,
commerceTax: this.commerceTax.level,
industryTax: this.industryTax.level,
rename: this.rename.level,
news: this.news.level,
};
};
City.prototype.date = function() {
return new Date(this.day * 24 * 60 * 60 * 1000).toDateString();
}
City.prototype.report = function() {
var update = document.createElement('div');
var close = document.createElement('div');
close.innerHTML = '<b>×</b>';
close.className = 'close';
close.addEventListener('click', function() {
this.newspaper.removeChild(update);
}.bind(this));
update.appendChild(close);
var text = document.createElement('div');
var resident = this.resident.demand / this.resident.capacity();
var html = '<b class="date">' + this.date() + '</b>';
html += '<h1>' + this.name + ' News</h1>';
html += '<h3>Opinion</h3>';
if (resident >= 1) {
var demand = this.transport.label.innerHTML;
html += 'Housing is currently in demand! Why won\'t the mayor zone some more residential';
if (this.residentTax.price() < this.currency) {
html += ' or raise that tax rate of ' + this.resident.tax + '%';
}
html += '? While on the subject of getting more citizens into the city, the proposal to <u>' + demand[0].toLowerCase() + demand.slice(1, demand.length - 1) + '</u> would likely help fill those built houses faster in the future.';
} else {
var demand = this.residentDemand.label.innerHTML;
html += 'Zoned residential is currently being unused. Has the mayor considered the proposal to <u>' + demand[0].toLowerCase() + demand.slice(1, demand.length - 1) + '</u> to encourage people to move in?';
}
html += '<br><br>';
var commerce = this.commerce.demand / this.commerce.capacity();
if (commerce >= 1) {
html += resident >= 1 ? 'Like' : 'Unlike';
html += ' residential, businesses want to set up shop! Now\'s the time to ';
if (this.commerceTax.price() < this.currency) {
html += 'either bump up that tax rate of ' + this.commerce.tax + '% or ';
}
html += 'add more commercial zones.';
} else {
var demand = this.commerceDemand.label.innerHTML;
html += resident >= 1 ? 'Unlike' : 'Like';
html += ' residential, zoned commercial still lies empty. When will the mayor <u>' + demand[0].toLowerCase() + demand.slice(1, demand.length - 1) + '</u> so businesses can function here?';
}
html += '<br><br>';
var industry = this.industry.demand / this.industry.capacity();
if (industry >= 1) {
html += 'Finally, why isn\'t the mayor expanding industry?';
if (this.industryTax.price() < this.currency) {
html += ' Is the mayor just going to increase the ' + this.industry.tax + '% taxes since there\'s not enough zones to meet demand? Harsh.';
}
} else {
var demand = this.industryDemand.label.innerHTML;
html += 'For reasons unclear to this reporter, the mayor seems to be considering whether to <u>' + demand[0].toLowerCase() + demand.slice(1, demand.length - 1) + '</u> to entice industry into the city. While high industrial and commercial demand drive up residential demand, one wonders if there is a better way.';
}
html += '<h3>Sports</h3>';
if (this.population == 0) {
html += this.name + ' doesn\'t yet have a sports time, as no one lives here yet.';
} else {
var us = Math.floor(Math.random() * 20);
var them = Math.floor(Math.random() * 20);
var team = teams[Math.floor(Math.random() * teams.length)];
var sport = sports[Math.floor(Math.random() * sports.length)];
if (us > them) {
html += 'Local sports team the <b>' + this.name + ' Llamas</b> handily defeated rival team the <b>' + team + '</b> <b>' + us + '</b> to <b>' + them + '</b> in <b>' + sport + '</b>. Go team!';
} else if (us < them) {
html += 'Local sports team the <b>' + this.name + ' Llamas</b> suffered a crushing defeat against rival team the <b>' + team + '</b>. The ' + sport + ' game ended with the Llamas down by <b>' + (them - us) + '</b>.';
if (them - us > 10) {
html += ' It was indeed a dark day for ' + this.name + '.';
}
} else {
html += 'After a long and well played ' + sport + ' match, local sports team the <b>' + this.name + ' Llamas</b> tied rival team the <b>' + team + '</b>. The <b>' + us + '</b> to <b>' + them + '</b> game will surely go down as one of the best in the history of the sport.';
}
}
text.innerHTML = html;
update.appendChild(text);
if (this.newspaper.firstChild) {
this.newspaper.insertBefore(update, this.newspaper.firstChild);
} else {
this.newspaper.appendChild(update);
}
}
City.prototype.spend = function(cost) {
if (cost <= this.currency) {
this.currency -= cost;
return true;
}
};
City.prototype.update = function(write) {
var ratio = Math.max(20,
Math.min(80, 10 + Math.log(this.population) * 4)) / 100;
this.resident.tax = 10 + this.residentTax.level;
this.commerce.tax = 15 + this.commerceTax.level;
this.industry.tax = 12 + this.industryTax.level;
this.commerce.demand = 1.1 * Math.max(1,
this.population * Math.max(.1, ratio - this.commerce.tax / 100) *
Math.pow(commerceMultiplier, this.commerceDemand.level));
this.industry.demand = 1.1 * Math.max(1,
this.population * Math.max(.1, 1 - ratio - this.industry.tax / 100) *
Math.pow(industryMultiplier, this.industryDemand.level));
this.resident.demand = Math.max(1,
Math.max(.1, 1 - this.resident.tax / 100) *
(this.commerce.demand + this.commerce.capacity() +
this.industry.demand + this.industry.capacity() / 4) *
Math.pow(residentMultiplier, this.residentDemand.level));
this.items.forEach(function(item) {
item.update();
});
this.tax = this.resident.income() + this.commerce.income() + this.industry.income();
if (write) {
this.currency += this.tax;
if (this.population < this.resident.capacity()) {
this.population += Math.pow(Math.max(1.01, 2 - this.resident.tax / 100),
this.transport.level);
}
}
output(this.status, {
City: this.name,
Population: format(this.population, '♥'),
Treasury: format(this.currency),
'Daily Income': format(this.tax),
}, this.date());
localStorage.setItem('cityclicker', btoa(JSON.stringify(this.data())));
};
var load = function() {
try {
return JSON.parse(atob(localStorage.getItem('cityclicker')));
} catch(e) {};
return {};
};
var data = load();
var city = new City(data);
var time = function() {
var start = new Date();
city.update(true);
city.day++;
setTimeout(time, Math.max(1000 - (new Date() - start), 100));
};
setTimeout(time, 1000);
})();