-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.html
213 lines (192 loc) · 5.97 KB
/
index.html
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
<!doctype html>
<meta charset="utf-8">
<title>Barro Calculator</title>
<link rel="stylesheet" href="reset.css">
<link rel="stylesheet" href="typebase.css">
<link rel="stylesheet" href="animate.css">
<link rel="stylesheet" href="whatnot.css">
<script src="lib/coffee-script.js"></script>
<script src="lib/jquery-2.1.4.js"></script>
<script src="helpers.coffee" type="text/coffeescript"></script>
<section>
<h2>Random Events</h2>
<button id="random-event">Make Something Happen</button>
<output id="random-event-output"></output>
</section>
<section>
<h2>Death</h2>
<label>
<span>Age</span>
<input type="number" min="0" id="reaping-age">
</label>
<label>
<span>Health</span>
<input type="number" min="0" max="10" id="reaping-health">
</label>
<button id="death-button"><img src="grim-reaper.svg" style="height:1.4em;vertical-align:bottom">?</button>
<output id="death-output"></output>
</section>
<section>
<h2>Birth</h2>
<div id="parents"></div>
<button id="now-mate">Now mate!</button>
<div id="birth-output"></div>
</section>
<a href="barrish">Barrish text</a>
<script type="text/coffeescript">
death_chance_modifier_for = (health, doublious=no)->
console.log "Get death_chance_modifier_for", health, doublious
1 - ((health - 5) / 10) * (if doublious then 1.9 else 1)
# lerp(1.5, 0.5, health / 10) * (if doublious then 1.9 else 1)
$.get "events.txt", (text)->
# parse events.txt
events =
for [event_name, event_chance] in colon_separated lines_of text
{name: event_name, chance: parseFloat event_chance}
# normalize chances
total_chance = 0
for event in events
total_chance += event.chance
for event in events
event.chance /= total_chance
console?.log events
get_random_event = ->
x = Math.random()
bx = 0
for event in events
if bx <= x <= bx += event.chance
return event
return event # ?
$ "#random-event"
.click ->
$ "#random-event-output"
.empty()
.append(
$ "<span class='animated bounceIn'>"
.text get_random_event().name
)
$.get "traits.txt", (text)->
# parse traits.txt
trait_names = lines_of text
# input
$ "#parents"
.append(
parents = for designation in ["Parent A", "Parent B"]
$ "<div class='person parent'>"
.append(
$ "<h3>"
.text designation
)
.append(
for trait_name in trait_names
$ "<label>"
.append(
$ "<span>"
.text trait_name
$ "<input type='number' min='0' max='10'>"
.on "change keydown keyup paste cut", (e)->
$(e.target).closest ".person"
.data trait_name, e.target.value
)
)
)
$ "#now-mate"
.click ->
parents = for el in $ ".parent"
parent = {}
for trait_name in trait_names
parent[trait_name] = parseFloat $(el).data trait_name
if isNaN parent[trait_name]
alert "Invalid value for trait #{trait_name}"
throw new Error "Invalid trait value for #{trait_name}"
parent
# average traits
newborn = {}
for trait_name in trait_names
newborn[trait_name] = 0
for parent in parents
for k, v of parent
newborn[k] += v
for trait_name in trait_names
newborn[trait_name] /= parents.length
average_health = newborn.Health
console.log "Average health: #{average_health}"
# chance of death :(
death_chance_modifier = death_chance_modifier_for average_health, (average_health > 5)
console.log "Death chance modifier: #{death_chance_modifier}"
death_chance = 0.230769 * death_chance_modifier
console.log "Death chance: #{death_chance}"
if Math.random() < death_chance
$ "#birth-output"
.html "<div class='person newborn child dead animated bounceIn'><h3>Dead Baby :(</h3></div>"
return
# vary traits
for trait_name in trait_names
newborn[trait_name] += (Math.random()*2-1) * 2
# output
$ "#birth-output"
.empty()
.append(
$ "<div class='person newborn child animated bounceIn'>"
.append(
$ "<h3>"
.text "Newborn"
)
.append(
for trait_name in trait_names
$ "<label>"
.text trait_name
.append(
$ "<input type='number' readonly='readonly'>"
.attr value: Math.round newborn[trait_name]
)
)
)
$ "#death-button"
.click ->
age = ($ "#reaping-age").val()
health = ($ "#reaping-health").val()
# Source: http://www.cdc.gov/nchs/data/dvs/hist290_0039.pdf
# ALL CAUSES, ALL RACES, "BOTH" SEXES, YEAR 1900
# ["16244.8", "1983.8", "385.9", "585.5", "819.8", "1023.1", "1495.4", "2723.6", "5636.1", "12330.0", "26088.2"]
# [1.62448, 0.19838, 0.03859, 0.05855, 0.08198, 0.10231, 0.14954, 0.27236, 0.56361, 1.233, 2.60882]
# ["Under 1", "year 1‐4", "years 5‐14", "years 15‐24", "years 25‐34", "years 35‐44", "years 45‐54", "years 55‐64", "years 65‐74", "years 75‐84", "85 years & over"]
death_chance =
switch
when age < 1
# 0.0162448
0.025 # (MODIFIED)
when age < 5
0.019838
when age < 15
0.003859
when age < 25
0.005855
when age < 35
0.008198
when age < 45
0.010231
when age < 55
0.014954
when age < 65
0.027236
when age < 75
0.056361
when age < 85
0.1233
else
0.260882
console.log "Unmodified death chance: #{death_chance}"
death_chance_modifier = death_chance_modifier_for health
console.log "Death chance modifier: #{death_chance_modifier}"
death_chance *= death_chance_modifier
console.log "Death chance: #{death_chance}"
death = (Math.random() < death_chance)
# output
$ "#death-output"
.empty()
.append(
$ "<span class='animated bounceIn'>"
.text if death then "Ye died" else "Ye survived"
)
</script>