-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbio.cs
288 lines (286 loc) · 8.1 KB
/
bio.cs
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
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
// color
using Microsoft.Xna.Framework;
namespace Bio {
class Lifeform {
public static readonly List<Lifeform> lifeforms = new List<Lifeform>();
readonly AnimalName name;
public readonly int mass; // in grams
readonly int maturity_time; // in days???
readonly string[] tags;
public Lifeform(AnimalName s, int mass, int mat, string[] t){
name = s;
this.mass = mass;
maturity_time = mat;
tags = t;
lifeforms.Append(this);
}
public static void ParseData(){
IEnumerable<string> raw = File.ReadAllLines("data/bio_part.dat")
.Concat(File.ReadAllLines("data/bio_plan.dat"))
.Concat(File.ReadAllLines("data/bio.dat"))
.Select(line =>
Regex.Replace(line.ToLower(), @"^\s+|\s+$", "") // set lowercase; remove leading/trailing whitespace
);
string type = "";
// MAKE SURE TO COPY THESE TO RESET
AnimalName name = AnimalName.none;
int mass = 0;
int maturity_time = 0;
ushort relsize = 0;
BodyPart parent = BodyPart.root;
BodyPart[] parts = new BodyPart[0];
BodyPlan body_plan = BodyPlan.none;
string[] tags = new string[0];
char c = '\0';
Color color = Color.Gray;
double dimorphism = 1;
Action Reset = () => {
name = AnimalName.none;
mass = 0;
maturity_time = 0;
relsize = 0;
parent = BodyPart.root;
parts = new BodyPart[0];
body_plan = BodyPlan.none;
tags = new string[0];
c = '\0';
color = Color.Gray;
dimorphism = 1;
};
int partcount = 0;
int plans = 0;
int animals = 0;
int plants = 0;
foreach (string line in raw){
string[] split = line.Split(" ");
string kw = split[0];
switch (kw){
case "part":
case "plan":
case "animal":
case "plant":
type = kw;
continue;
case "name":
name = new AnimalName(split[1]);
continue;
case "tags":
tags = split.Skip(1).ToArray();
continue;
case "parent":
parent = BodyPart.FromName(split[1]);
continue;
case "parts":
parts = split.Skip(1).Select(s => BodyPart.FromName(s)).ToArray();
continue;
case "icon":
color = Program.ColorFromHex(split[1]);
c = split[2][0];
continue;
case "mass":
mass = int.Parse(split[1]);
continue;
case "maturity_time":
maturity_time = int.Parse(split[1]);
continue;
case "relsize":
relsize = ushort.Parse(split[1]);
continue;
case "dimorphism":
dimorphism = double.Parse(split[1]);
continue;
case "template":
case "bodyplan":
body_plan = BodyPlan.FromName(split[1]);
continue;
case "end":
break; // handled below
default: // just a comment!
continue;
}
// handle end
if (type == "part"){
new BodyPart(name, parent, tags, relsize);
partcount++;
}
else if (type == "plan"){
new BodyPlan(name, body_plan, parts, tags);
plans++;
}
else if (type == "animal"){
new Animal(name, mass, maturity_time, tags, body_plan, c, color, dimorphism);
animals++;
}
else if (type == "plant"){
new Plant(name, mass, maturity_time, tags);
plants++;
}
else
throw new NotImplementedException();
// reset
Reset();
}
Program.Log(String.Format("{0} bodyparts loaded", partcount), 0);
Program.Log(String.Format("{0} bodyplans loaded", plans), 0);
Program.Log(String.Format("{0} plants loaded", plants), 0);
Program.Log(String.Format("{0} animals loaded", animals), 0);
}
}
class Animal : Lifeform {
static readonly List<Animal> animals = new List<Animal>();
static readonly double standardDeviationAsAFactorOfMass = 0.16; // sigma = 0.16*mean
readonly BodyPlan bodyplan;
char c;
Color color;
double dimorphismConstant;
public Animal(AnimalName n, int mas, int mat, string[] t, BodyPlan p, char c, Color col, double d) : base(n, mas, mat, t){
bodyplan = p;
this.c = c;
color = col;
dimorphismConstant = d;
animals.Append(this);
}
int RandomMass(){
// normally distributed.
return (int)MochaRandom.Normal(mass, standardDeviationAsAFactorOfMass*mass);
}
}
class AnimalName {
public readonly string generic, generic_pl;
readonly string male_, male_pl_, female_, female_pl_,
young_, young_pl_, male_young_, male_young_pl_, female_young_, female_young_pl_;
public static readonly AnimalName none = new AnimalName("unnamed");
// HAS ALL NAMES
public AnimalName(string g, string gp, string m, string mp, string f, string fp,
string y, string yp, string my, string myp, string fy, string fyp){
// adult
generic = g;
generic_pl = gp;
male_ = m;
male_pl_ = mp;
female_ = f;
female_pl_ = fp;
// young
young_ = y;
young_pl_ = yp;
male_young_ = my;
male_young_pl_ = myp;
female_young_ = fy;
female_young_pl_ = fyp;
}
// ALL NAMES BUT GENDERED YOUNG
public AnimalName(string g, string gp, string m, string mp, string f, string fp, string y, string yp){
// adult
generic = g;
generic_pl = gp;
male_ = m;
male_pl_ = mp;
female_ = f;
female_pl_ = fp;
// young
young_ = male_young_ = female_young_ = y;
young_pl_ = male_young_pl_ = female_young_pl_ = yp;
}
// ALL NAMES BUT YOUNG
public AnimalName(string g, string gp, string m, string mp, string f, string fp){
// adult
generic = young_ = g;
generic_pl = young_pl_ = gp;
male_ = male_young_ = m;
male_pl_ = male_young_pl_ = mp;
female_ = female_young_ = f;
female_pl_ = female_young_pl_ = fp;
}
// ADULT/YOUNG DISTINCTION ONLY
public AnimalName(string g, string gp, string y, string yp){
// adult
generic = male_ = female_ = g;
generic_pl = male_pl_ = female_pl_ = gp;
// young
young_ = male_young_ = female_young_ = y;
young_pl_ = male_young_pl_ = female_young_pl_ = yp;
}
// SINGULAR/PLURAL ONLY
public AnimalName(string g, string gp){
generic = male_ = female_ = young_ = male_young_ = female_young_ = g;
generic_pl = male_pl_ = female_pl_ = young_pl_ = male_young_pl_ = female_young_pl_ = gp;
}
// NO DISTINCTIONS OF ANY KIND
public AnimalName(string s){
generic = male_ = female_ = young_ = male_young_ = female_young_ =
generic_pl = male_pl_ = female_pl_ = young_pl_ = male_young_pl_ = female_young_pl_ = s;
}
}
class BodyPart {
public static readonly List<BodyPart> bodyParts = new List<BodyPart>();
readonly AnimalName name;
readonly BodyPart parent;
readonly string[] tags;
readonly ushort relsize; // this is primarily based on average human mass in grams
/* Sources:
https://exrx.net/Kinesiology/Segments
https://www.hindawi.com/journals/ari/2018/4687538/
*/
public BodyPart(AnimalName n, BodyPart p, string[] t, ushort r){
name = n;
parent = p;
tags = t;
relsize = r;
bodyParts.Add(this);
}
BodyPart(string n, ushort r){
name = new AnimalName(n);
parent = root;
tags = new string[0];
relsize = r;
bodyParts.Add(this);
}
double massFraction {
get { return (double)relsize/root.relsize; }
}
public static BodyPart FromName(string s){
return bodyParts.Find(bp => bp.name.generic == s);
}
public static readonly BodyPart root = new BodyPart("root", 62000);
}
class BodyPlan {
public static readonly List<BodyPlan> bodyPlans = new List<BodyPlan>();
AnimalName name;
BodyPart[] parts;
string[] tags;
BodyPlan(string n){
name = new AnimalName(n);
tags = new string[0];
parts = new BodyPart[0];
bodyPlans.Append(this);
}
public BodyPlan(AnimalName n, BodyPlan b, BodyPart[] p, string[] t){
name = n;
if (b == null){
parts = p;
tags = t;
}
else {
parts = b.parts.Concat(p).ToArray();
tags = t.Concat(b.tags).ToArray();
}
bodyPlans.Append(this);
}
public static BodyPlan FromName(string s){
return bodyPlans.Find(bp => bp.name.generic == s);
}
// useful templates
public static readonly BodyPlan none = new BodyPlan("none");
}
class Plant : Lifeform{
static readonly List<Plant> plants = new List<Plant>();
public Plant(AnimalName n, int m, int mt, string[] t) : base(n, m, mt, t){
plants.Append(this);
}
}
}