forked from andbof/yastg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
planet.c
164 lines (142 loc) · 3.7 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
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <assert.h>
#include <pthread.h>
#include "base.h"
#include "common.h"
#include "data.h"
#include "stringtree.h"
#include "log.h"
#include "mtrandom.h"
#include "parseconfig.h"
#include "planet.h"
#include "ptrlist.h"
#include "sector.h"
#include "universe.h"
static void planet_init(struct planet *p)
{
memset(p, 0, sizeof(*p));
ptrlist_init(&p->bases);
ptrlist_init(&p->stations);
ptrlist_init(&p->moons);
INIT_LIST_HEAD(&p->list);
}
void planet_free(struct planet *p)
{
assert(p != NULL);
struct base *b;
struct planet *m;
struct list_head *lh;
ptrlist_for_each_entry(b, &p->bases, lh)
base_free(b);
ptrlist_free(&p->bases);
ptrlist_for_each_entry(b, &p->stations, lh)
base_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);
}
int planet_load(struct planet *p, struct config *ctree)
{
struct base *b;
while (ctree) {
if (strcmp(ctree->key, "NAME") == 0) {
p->name = strdup(ctree->data);
} else if (strcmp(ctree->key, "TYPE") == 0) {
p->type = ctree->data[0];
} else if (strcmp(ctree->key, "BASE") == 0) {
b = malloc(sizeof(*b));
if (!b)
return -1;
loadbase(b, ctree->sub);
ptrlist_push(&p->bases, b);
}
ctree = ctree->next;
}
return 0;
}
#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 sector *sector)
{
planet->sector = sector;
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;
do {
planet->type = mtrandom_uint(PLANET_TYPE_N);
} while (planet_types[planet->type].zones[zone] == 0);
struct planet_type *type = &planet_types[planet->type];
planet->dia = mtrandom_uint(type->maxdia - type->mindia) + type->mindia;
planet->life = mtrandom_uint(type->maxlife - type->minlife) + type->minlife;
switch (zone) {
case HOT:
planet->dist = mtrandom_uint(sector->hablow);
break;
case ECO:
planet->dist = mtrandom_uint(sector->habhigh - sector->hablow) + sector->hablow;
break;
case COLD:
/* FIXME: Some kind of logarithmic function ... ? */
planet->dist = mtrandom_uint(PLANET_DIST_MAX - sector->habhigh) + sector->habhigh;
break;
default:
bug("%s", "illegal execution point");
}
base_populate_planet(planet);
}
int planet_populate_sector(struct sector* sector)
{
struct planet *p;
int num = planet_gennum();
pthread_rwlock_wrlock(&univ.planetnames_lock);
for (int i = 0; i < num; i++) {
p = malloc(sizeof(*p));
if (!p)
return -1;
planet_init(p);
planet_genesis(p, sector);
p->name = malloc(strlen(sector->name) + ROMAN_LEN + 2);
if (!p->name) {
free(p);
return -1;
}
sprintf(p->name, "%s %s", sector->name, roman[i]); /* FIXME: Wait with naming until all are made, sort on mean distance from sun. */
ptrlist_push(§or->planets, p);
st_add_string(&univ.planetnames, p->name, p);
if (p->gname)
st_add_string(&univ.planetnames, p->gname, p);
}
pthread_rwlock_unlock(&univ.planetnames_lock);
return 0;
}