-
Notifications
You must be signed in to change notification settings - Fork 2
/
system.c
98 lines (77 loc) · 1.78 KB
/
system.c
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
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "common.h"
#include "log.h"
#include "universe.h"
#include "civ.h"
#include "system.h"
#include "planet.h"
#include "port.h"
#include "ptrlist.h"
#include "parseconfig.h"
#include "star.h"
void system_init(struct system *s)
{
memset(s, 0, sizeof(*s));
s->phi = 0.0;
rb_init_node(&s->x_rbtree);
ptrlist_init(&s->stars);
ptrlist_init(&s->planets);
ptrlist_init(&s->ports);
ptrlist_init(&s->links);
INIT_LIST_HEAD(&s->list);
}
void system_free(struct system *s)
{
struct list_head *lh;
struct star *sol;
struct planet *planet;
struct port *port;
free(s->name);
free(s->gname);
ptrlist_for_each_entry(sol, &s->stars, lh)
star_free(sol);
ptrlist_free(&s->stars);
ptrlist_for_each_entry(planet, &s->planets, lh)
planet_free(planet);
ptrlist_free(&s->planets);
ptrlist_for_each_entry(port, &s->ports, lh)
port_free(port);
ptrlist_free(&s->ports);
ptrlist_free(&s->links);
free(s);
}
#define STELLAR_MUL_HAB -50
int system_create(struct system *s, char *name)
{
struct star *sol;
struct list_head *lh;
system_init(s);
s->name = strdup(name);
if (star_populate_system(s)) {
free(s->name);
return -1;
}
s->hab = 0;
unsigned int totlum = 0;
ptrlist_for_each_entry(sol, &s->stars, lh) {
s->hab += sol->hab;
totlum += sol->lumval;
}
s->hablow = star_gethablow(totlum);
s->habhigh = star_gethabhigh(totlum);
if (planet_populate_system(s))
return -1;
printf(" Number of planets: %lu\n", ptrlist_len(&s->planets));
return 0;
}
unsigned long system_distance(const struct system * const a, const struct system * const b)
{
long result = sqrt( (double)(b->x - a->x)*(b->x - a->x) +
(double)(b->y - a->y)*(b->y - a->y) );
if (result < 0)
return -result;
return result;
}