-
Notifications
You must be signed in to change notification settings - Fork 0
/
emojinomics.js
182 lines (154 loc) · 4.41 KB
/
emojinomics.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
// land (not shown, limits land use, relates to roaming livestock cap)
// mountains / earth
// -apple -earth +apple-tree +apple-cap
// over time, apple trees yield apples up to the capacity
// -earth +seedling +livestock-cap
// -money +sheep
// sheep follow logistic function, breeding until population approaches livestock cap
// population
// -food per turn
// land
// appleTree
// apple
// appleCapacity = appleTree * 10
// population' = population
// buttons:
//
const updaters = [];
// const counts = {population: 0, apple: 0, appleCap: 10, appleTree: 0};
// const changers = {apple: {}, appleTree: {apple: 1}};
// const costs = {apple: {}, appleTree: {apple: 10}};
// const rules = {
// appleTree: {
// apple: -10,
// appleTree: 1,
// appleCap: 10,
// }
// };
const magnitudes = ['', 'K', 'G', 'P'];
const human = n => {
let i = 0;
if (n < 1e3) {
return n;
}
do {
n = (n / 1e3);
i++;
} while (n > 1e3);
if (n < 10) {
n = n.toFixed(2);
} else if (n < 100) {
n = n.toFixed(1);
}
return n + magnitudes[i];
}
const resourceForElement = el => {
do {
const resource = el.dataset.resource;
if (resource) {
return resource;
}
el = el.parentElement;
} while (el != null);
};
// const funded = resource => Object.entries(costs[resource])
// .every(([resource, amount]) => counts[resource] > amount);
// const makeIncrementerUpdater = (resource, el) => {
// return () => {
// el.disabled = !funded(resource);
// };
// };
// const hookupIncrementer = el => {
// const product = resourceForElement(el);
// el.addEventListener('click', () => {
// if (funded(product)) {
// for (const [resource, amount] of Object.entries(costs[product])) {
// counts[resource] -= amount;
// }
// counts[product]++;
// draw();
// }
// });
// updaters.push(makeIncrementerUpdater(product, el));
// };
// for (const el of document.querySelectorAll(".incrementer")) {
// hookupIncrementer(el);
// }
const delay = ms => new Promise(resolve => setTimeout(resolve, ms));
const draw = () => {
for (const update of updaters) {
update();
}
};
const round = n => {
const min = Math.floor(n);
const mid = n - min;
const max = Math.ceil(n);
return Math.random() > mid ? min : max;
};
const grow = (at, cap, damp) => round(Math.max(0, Math.min(cap, at + round(at * (cap - at) / cap * Math.pow(Math.random(), 8)))));
const consume = (creature, growth, food, foodPerCreature) => {
const maxFoodEaten = Math.min(food, creature * growth * foodPerCreature);
const maxCreature = Math.min(creature * growth, maxFoodEaten / foodPerCreature);
const foodEaten = maxCreature * foodPerCreature;
// TODO even out this rounding
return [round(maxCreature), round(food - foodEaten)];
};
const generate = state => {
const { land, mountain, pineTree, appleTree, apple, pineApple, worm, bird } = state;
const availableLand = land - mountain - pineTree - appleTree;
const mountainous = mountain / land;
const pineTreeTarget = mountain * 2 * (land - mountain) / land;
const appleTreeTarget = (land - mountain - pineTreeTarget) / 2;
const [ worm2, apple2 ] = consume(worm, 1.2, apple, 0.5);
const [ bird2, worm3 ] = consume(bird, 1.1, worm2, 3);
const next = {
land,
mountain,
pineTree: grow(pineTree, pineTreeTarget, 4),
pineApple: grow(pineApple + 1, pineTree * 10, 8),
appleTree: grow(appleTree, appleTreeTarget, 8),
apple: Math.max(0, grow(apple2 + 1, appleTree * 10, 8)),
worm: worm3,
bird: bird2,
};
console.clear();
console.table(next);
return next;
};
const main = async () => {
const makeCounterUpdater = el => {
const resource = resourceForElement(el);
return () => {
el.innerText = human(state[resource]);
};
};
for (const el of document.querySelectorAll(".counter")) {
updaters.push(makeCounterUpdater(el));
}
const makeDisplayCounterUpdater = el => {
const resource = resourceForElement(el);
return () => {
el.style.display = state[resource] ? 'block' : 'none';
};
};
for (const el of document.querySelectorAll(".displayCounter")) {
updaters.push(makeDisplayCounterUpdater(el));
}
let state = {
land: 100,
mountain: 10,
pineTree: 1,
pineApple: 0,
appleTree: 1,
apple: 20,
worm: 10,
bird: 1,
};
while (window.break === undefined) {
state = generate(state);
draw()
await delay(1000);
}
};
main();