]> git.lizzy.rs Git - plan9front.git/blob - sys/src/games/galaxy/galaxy.h
games/galaxy: add n-body simulator
[plan9front.git] / sys / src / games / galaxy / galaxy.h
1 typedef struct QB QB;
2 typedef struct Body Body;
3 typedef struct Quad Quad;
4 typedef struct Vector Vector;
5
6 struct QB {
7         union {
8                 Quad *q;
9                 Body *b;
10         };
11         int t;
12 };
13
14 struct Vector {
15         double x, y;
16 };
17
18 struct Body {
19         Vector;
20         Vector v, a, newa;
21         double size, mass;
22         Image *col;
23 };
24
25 struct Quad {
26         Vector;
27         QB c[4];
28         double mass;
29 };
30
31 #pragma varargck type "B" Body*
32
33 struct {
34         QLock;
35         Body *a;
36         int l, max;
37 } glxy;
38
39 struct {
40         Quad *a;
41         int l, max;
42 } quads;
43
44 #define π2 6.28318530718e0
45
46 enum {
47         EMPTY,
48         QUAD,
49         BODY,
50 };
51
52 Point orig;
53 double G, θ, scale, ε, LIM, dt, dt²;
54 Body ZB;
55 QB space;
56
57 Image *randcol(void);
58
59 Body *body(void);
60 void drawbody(Body*);
61 Vector center(void);
62 void glxyinit(void);
63 void readglxy(int);
64 void writeglxy(int);
65 int Bfmt(Fmt*);
66
67 void quadcalc(QB, Body*, double);
68 int quadins(Body*, double);
69 void growquads(void);
70 void quadsinit(void);
71
72 int stats;
73 int quaddepth;
74 int calcs;
75 double avgcalcs;
76
77 #define STATS(e) if(stats) {e}
78
79 #define CHECKLIM(b, f) \
80         if(((f) = fabs((b)->x)) > LIM/2)        \
81                 LIM = (f)*2;    \
82         if(((f) = fabs((b)->y)) > LIM/2)        \
83                 LIM = (f)*2