-
Notifications
You must be signed in to change notification settings - Fork 2
/
planet.c
173 lines (151 loc) · 3.8 KB
/
planet.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
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <assert.h>
#include <pthread.h>
#include "universe.h"
#include "planet.h"
#include "common.h"
#include "log.h"
#include "mtrandom.h"
#include "port.h"
#include "parseconfig.h"
#include "planet_type.h"
#include "ptrlist.h"
#include "stringtrie.h"
#include "system.h"
static void planet_init(struct planet *p)
{
memset(p, 0, sizeof(*p));
ptrlist_init(&p->ports);
ptrlist_init(&p->stations);
ptrlist_init(&p->moons);
INIT_LIST_HEAD(&p->list);
}
void planet_free(struct planet *p)
{
assert(p != NULL);
struct port *b;
struct planet *m;
struct list_head *lh;
ptrlist_for_each_entry(b, &p->ports, lh)
port_free(b);
ptrlist_free(&p->ports);
ptrlist_for_each_entry(b, &p->stations, lh)
port_free(b);
ptrlist_free(&p->stations);
ptrlist_for_each_entry(m, &p->moons, lh)
planet_free(m);
ptrlist_free(&p->moons);
if (p->name) {
st_rm_string(&univ.planetnames, p->name);
free(p->name);
}
if (p->gname) {
st_rm_string(&univ.planetnames, p->gname);
free(p->gname);
}
free(p);
}
#define PLANET_MUL_ODDS 2
#define PLANET_MUL_MAX 10
static int planet_gennum()
{
int num = 1;
unsigned int ran;
while ((ran = mtrandom_uint(UINT_MAX)) < UINT_MAX/PLANET_MUL_ODDS)
num++;
if (num > PLANET_MUL_MAX)
num = PLANET_MUL_MAX;
return num;
}
#define PLANET_HOT_ODDS 3
#define PLANET_ECO_ODDS 5
#define PLANET_COLD_ODDS 10
#define PLANET_DIST_MAX (100 * GM_PER_AU) /* Based on sol system */
static void planet_genesis(struct planet *planet, struct system *system)
{
planet->system = system;
unsigned int tmp = mtrandom_uint(PLANET_HOT_ODDS + PLANET_ECO_ODDS + PLANET_COLD_ODDS);
enum planet_zone zone;
if (tmp <= PLANET_HOT_ODDS)
zone = HOT;
else if (tmp > PLANET_HOT_ODDS && tmp <= PLANET_HOT_ODDS + PLANET_ECO_ODDS)
zone = ECO;
else
zone = COLD;
unsigned int i, j;
do {
i = mtrandom_uint(list_len(&univ.planet_types));
j = 0;
list_for_each_entry(planet->type, &univ.planet_types, list) {
if (j++ == i)
break;
}
} while (planet->type->zones[zone] == 0);
planet->dia = mtrandom_uint(planet->type->maxdia - planet->type->mindia) + planet->type->mindia;
planet->life = mtrandom_uint(planet->type->maxlife - planet->type->minlife) + planet->type->minlife;
switch (zone) {
case HOT:
planet->dist = mtrandom_uint(system->hablow);
break;
case ECO:
planet->dist = mtrandom_uint(system->habhigh - system->hablow) + system->hablow;
break;
case COLD:
/* FIXME: Some kind of logarithmic function ... ? */
planet->dist = mtrandom_uint(PLANET_DIST_MAX - system->habhigh) + system->habhigh;
break;
default:
bug("%s", "illegal execution point");
}
port_populate_planet(planet);
}
static int cmp_planet_distances(const void *_p1, const void *_p2, void *data)
{
const struct planet *p1 = _p1;
const struct planet *p2 = _p2;
if (p1->dist < p2->dist)
return -1;
else if (p1->dist > p2->dist)
return 1;
else
return 0;
}
int planet_populate_system(struct system* system)
{
struct planet *p;
int num = planet_gennum();
int i;
pthread_rwlock_wrlock(&univ.planetnames_lock);
for (i = 0; i < num; i++) {
p = malloc(sizeof(*p));
if (!p)
goto err;
planet_init(p);
planet_genesis(p, system);
ptrlist_push(&system->planets, p);
}
ptrlist_sort(&system->planets, NULL, cmp_planet_distances);
struct list_head *lh;
i = 0;
ptrlist_for_each_entry(p, &system->planets, lh) {
p->name = malloc(strlen(system->name) + ROMAN_LEN + 2);
if (!p->name) {
free(p);
goto err;
}
sprintf(p->name, "%s %s", system->name, roman[i]);
st_add_string(&univ.planetnames, p->name, p);
if (p->gname)
st_add_string(&univ.planetnames, p->gname, p);
i++;
}
pthread_rwlock_unlock(&univ.planetnames_lock);
return 0;
err:
ptrlist_free(&system->planets);
pthread_rwlock_unlock(&univ.planetnames_lock);
return -1;
}